The Qt program setting interface is displayed in the center of the screen (including the processing of multi-screen machines)

        I recently encountered a problem in the program. I searched the Internet for a long time and did not find a similar problem, but I still found a few related blog posts to solve it, so I hereby write this problem and the solution here for convenience Those who encountered similar problems later were inspired.

       When we write the interface, it will be displayed in the center of the screen, because it is really inconvenient to run around. I have been using the following code before:

 

int main(int argc, char * argv[])
{
    QApplication app(argc, argv);
    Window window;//这个类继承自QWidget
    window.move((app.desktop()->width() - window.width()) / 2, (app.desktop()->height() - window.height()) / 2); 
    window.show();
    return app.exec();
}

        This piece of code is no problem. But because most of the customers they are facing are financial analysts and traders. In order to see the real-time market more intuitively and comprehensively, their computers are multi-screen machines, ranging from 2 displays to 8 screens (2* 4). When they use the program, their unanimous feedback is that the program interface is always incomplete and it is inconvenient to view. Something like this:

 

After consulting a lot of information, I found a most fundamental class QDesktopWidget. For a detailed description of this class, please refer to the Qt Assistant document or this blog QDesktopWidget detailed description . Put the code of a small program directly below, copy it and use it: 

 

 
  1. int main(int argc, char * argv[])

  2. {

  3. QApplication a(argc, argv);

  4. screenTest widget;

  5. widget.show();

  6. return a.exec();

  7. }

  8.  
  9. void screenTest::showInfo()//自己定义的函数,用于显示信息

  10. {

  11. QDesktopWidget * desktop = QApplication::desktop();

  12.  
  13. //获取程序所在屏幕是第几个屏幕

  14. int current_screen = desktop->screenNumber(this);

  15. //获取程序所在屏幕的尺寸

  16. QRect rect = desktop->screenGeometry(current_screen);

  17. //获取所有屏幕总大小

  18. QRect rectA = desktop->geometry();

  19. //获取所有屏幕的个数

  20. int screen_count = desktop->screenCount();

  21. //获取主屏幕是第几个

  22. int prim_screen = desktop->primaryScreen();

  23.  
  24. QString temp = "total screen size = " + QString::number(screen_count);

  25.     temp = temp + "\ncurrent screen num = " + QString::number(current_screen);

  26.     temp = temp + "\ncurrent screen rect " + QString::number(rect.width()) + "*" + QString::number(rect.height());

  27.     temp = temp + "\nwhole screen rect " + QString::number(rectA.width()) + "*" + QString::number(rectA.height());

  28. }

       That's it. Isn't it simple? Hope to help you, compare heart.

Guess you like

Origin blog.csdn.net/jiesunliu3215/article/details/108793066