How to get the screen size in Qt.
First call a static method in the Qapplication class, as follows:
Qdesktopwidget *qapplication::d esktop ()
The Qapplication class is used to manage the control flow and primary settings for GUI applications, with the following derived relationships:
QApplication:QGuiApplication:QCoreApplication:QObject
In the main program, it must be qapplication rather than qguiapplication or qcoreapplication, otherwise there will be
Error "Qwidget:cannot create a Qwidget without qapplication".
int main (int argc, char *argv[])
{
qapplication app (argc, argv);
// ...
return app.exec ();
}
The Qdesktopwidget class provides an interface for accessing multi-screen information, and sometimes our system consists of multiple screens, which can be treated as multiple desktops or as a large virtual desktop.
>>qdesktopwidget::screengeometry () to get the screen size, there are three overloaded functions:
Const Qrect qdesktopwidget::screengeometry (int screen =-1) const
Const Qrect qdesktopwidget::screengeometry (const qwidget *widget) const
Const Qrect qdesktopwidget::screengeometry (const qpoint &p) const
>>qdesktopwidget::availablegeometry () is used to obtain a valid area of the screen, such as removing the occupied area of the Desktop menu bar on the basis of screengeometry (), there are three overloaded functions:
Const Qrect qdesktopwidget::availablegeometry (int screen =-1) const
Const Qrect qdesktopwidget::availablegeometry (const qwidget *widget) const
Const Qrect qdesktopwidget::availablegeometry (const qpoint &p) const
>>qdesktopwidget inherits from Qwidget, so the screen size is also available through the width () and height () in Qwidget, but when there are multiple screens, the two interfaces are not used because the size of the entire large virtual desktop is returned.
The following is a complete example that shows how to get the screen size and set it to the QML property so that it can be accessed in the Qml file.
#include <QApplication> #include <QQuickView> #include <QtQml> #include <QDesktopWidget> #
Include <QRect> #include <iostream> int main (int argc, char *argv[]) {qapplication app (argc, argv);
int virtualwidth = 0;
int virtualheight = 0;
int availablewidth = 0;
int availableheight = 0;
int screenwidth = 0;
int screenheight = 0;
Qdesktopwidget *deskwgt = qapplication::d esktop ();
if (deskwgt) {virtualwidth = Deskwgt->width ();
Virtualheight = Deskwgt->height ();
Std::cout << "virtual width:" << virtualwidth << "Height:" << virtualheight << Std::endl;
Qrect availablerect = Deskwgt->availablegeometry ();
Availablewidth = Availablerect.width ();
Availableheight = Availablerect.height (); Std::cout << "Available width:" <<availablewidth << ", Height:" << availableheight << std::
Endl Qrect sCreenrect = Deskwgt->screengeometry ();
ScreenWidth = Screenrect.width ();
ScreenHeight = Screenrect.height ();
Std::cout << "screen width:" <<screenwidth << ", Height:" << screenheight << Std::endl;
} Qquickview view;
View.setsource (Qurl (qstringliteral ("qrc:///main.qml"));
if (View.rootcontext ()) {View.rootcontext ()->setcontextproperty ("Virtualwidth", virtualwidth);
View.rootcontext ()->setcontextproperty ("Virtualheight", virtualheight);
View.rootcontext ()->setcontextproperty ("Availablewidth", availablewidth);
View.rootcontext ()->setcontextproperty ("Availableheight", availableheight);
View.rootcontext ()->setcontextproperty ("ScreenWidth", screenwidth);
View.rootcontext ()->setcontextproperty ("ScreenHeight", screenheight);
} view.show ();
return App.exec (); }