"QT" gets the screen size in QT

Source: Internet
Author: User

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 (); }

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.