QT Window Screen Center display (with special qdesktopwidget, first calculated and displayed)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.