Not able to get screen resolution with QDesktopWidget.

  • Why doesn't the following code display the screen resolution :

      QMessageBox* EyeCare::constructMessageBox(QWidget *parent,QMessageBox *message,
                                      int x,int y,int w,int h,
                                      QString style){
        message = new QMessageBox(parent) ;
        return message ;
    void EyeCare::setupSimpleAndErrorMessage(){
        showSimpleMessage = constructMessageBox(this,showSimpleMessage,400,400,0,0
                                                 "font: 75 bold 12pt Arial;") ;
        QDesktopWidget widget ;
        setupSimpleAndErrorMessage() ;
        QRect screenSize = widget.availableGeometry(widget.primaryScreen());
        showSimpleMessage->setText("Height: " + screenSize.height()  );

    Instead of displaying the height. It displays the current location of my qt project.
    what is wrong with it ??
    please help...

  • Hi, try this:

    QDesktopWidget* widget = qApp->desktop();
    setupSimpleAndErrorMessage() ;
    QRect screenSize = widget->availableGeometry(widget->primaryScreen());
    showSimpleMessage->setText("Height: " + screenSize.height()  );

  • @hskoglund Still displaying the location of my qt project. :(

  • Hmm, I tested the code like this: created a new empty Widgets app, then modified mainwindow.cpp so it looks like this:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "QDesktopWidget"  // <-- added
    #include "qdebug.h"        // <-- added
    MainWindow::MainWindow(QWidget *parent) :
        ui(new Ui::MainWindow)
    // added these 3 lines:
        QDesktopWidget* widget = qApp->desktop();
        QRect screenSize = widget->availableGeometry(widget->primaryScreen());
        qDebug() << "Height: " << screenSize.height();
        delete ui;

    Debug output shows height of my desktop (minus 30 because of Windows 7's taskbar)

  • @hskoglund okay mine displays 1030 which means it subtracts 50 because of windows 10 taskbar. But why doesn't it display the same if use QMessageBox ? does your display it using QMessageBox ??

  • Lifetime Qt Champion


    Did you update your code following @hskoglund suggestions ? If so, can you post the new version ?

  • @SGaist yes but i removed it because now i am facing a new problem and here its.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.