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) ;
        message->setGeometry(x,y,w,h);
        message->setStyleSheet(style);
        return message ;
    }
    
    void EyeCare::setupSimpleAndErrorMessage(){
    
        showSimpleMessage = constructMessageBox(this,showSimpleMessage,400,400,0,0
                                                ,"color:#D3D3D3;background-color:rgb(32,52,60);"
                                                 "font: 75 bold 12pt Arial;") ;
    }
    
        QDesktopWidget widget ;
        setupSimpleAndErrorMessage() ;
        QRect screenSize = widget.availableGeometry(widget.primaryScreen());
        showSimpleMessage->setText("Height: " + screenSize.height()  );
        showSimpleMessage->show();
    

    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()  );
    showSimpleMessage->show();
    


  • @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) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
    // added these 3 lines:
        QDesktopWidget* widget = qApp->desktop();
        QRect screenSize = widget->availableGeometry(widget->primaryScreen());
        qDebug() << "Height: " << screenSize.height();
    }
    
    MainWindow::~MainWindow()
    {
        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

    Hi,

    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