Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Qt How to get the screen size?

 

There are multiple ways to get information about the screen size, as shown in the code below.

 

#include <iostream>
#include <QApplication>
#include <QDesktopWidget>

int main(int argc, char* argv[])
{
  //Must create a dummy application for QApplication::desktop() to work
  QApplication a(argc,argv);
  const int width = QApplication::desktop()->width();
  const int height = QApplication::desktop()->height();
  const QRect rect = QApplication::desktop()->rect();

  const int left = rect.left();
  const int right = rect.right();
  const int bottom = rect.bottom();
  const int top = rect.top();

  std::cout
    << "Width: " << width << '\n'
    << "Height: " << height << '\n'
    << "Left (x1): " << left << '\n'
    << "Top (y1): " << top << '\n'
    << "Right (x2): " << right << '\n'
    << "Bottom (y2): " << bottom << '\n'
    << "QRect width: " << rect.width() << '\n'
    << "QRect height: " << rect.height() << '\n';
}

 

Screen output:

 

Width: 1024
Height: 768
Left (x1): 0
Top (y1): 0
Right (x2): 1023
Bottom (y2): 767
QRect width: 1024
QRect height: 768

 

Project file:

 

#-------------------------------------------------
#
# Project created by QtCreator 2010-05-24T14:19:24
#
#-------------------------------------------------
TARGET = CppGetScreenSize
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict