The Screen center of the window displays the problem, the same principle in each development tool, first use a specific method to get the width and height of the display screen, and then calculate the window's upper-left coordinate position according to the width and height of the window itself.
QT can be used in two ways to reach the screen center of the window display:
Method one: Calculate the window before displaying the seat, and move to the corresponding seat and then display the window
int Main (int argc, char *argv[])
{
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB18030"));
Qapplication A (argc, argv);
Qdesktopwidget *pdesk = qapplication::d esktop ();
Cvxmainwindow W (pdesk);
W.move ((Pdesk->width ()-w.width ())/2, (Pdesk->height ()-w.height ())/2);
W.show ();
return a.exec ();
}
In the construction method of the form class, if you use Setfixedsize (800, 600); Set the fixed size of the window, you can run the program to achieve the center of the window display, or the window can not be centered in the desired display on the screen, you need to use the following method, before moving and display the window to reset the size of the window.
int Main (int argc, char *argv[])
{
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB18030"));
Qapplication A (argc, argv);
Qdesktopwidget *pdesk = qapplication::d esktop ();
Cvxmainwindow W (pdesk);
w.resize (800, 600);
W.move ((Pdesk->width ()-w.width ())/2, (Pdesk->height ()-w.height ())/2);
W.show ();
return a.exec ();
}
Method Two: The window is displayed and then moved to the appropriate seat
int Main (int argc, char *argv[])
{
QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GB18030"));
Qapplication A (argc, argv);
Qdesktopwidget *pdesk = qapplication::d esktop ();
Cvxmainwindow W (pdesk);
W.show ();
W.move ((Pdesk->width ()-w.width ())/2, (Pdesk->height ()-w.height ())/2);
return a.exec ();
}
First display after the move is easy to generate window flicker, I do not recommend the use of method two.
Attention:
1 using qtextcodec::setcodecfortr (Qtextcodec::codecforname ("GB18030"));
Please introduce: #include
2 cannot convert parameter 1 from "Qdesktopwidget *" to "Qwidget *"
Please introduce: #include
Http://blog.chinaunix.net/uid-20718335-id-364404.html
QT Window Screen Center display (with special qdesktopwidget, first calculated and displayed)