How to retrieve laptop's 'Display resolution'?
-
I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:
Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the
QDesktopWidget
class is obsolete, I want to avoid using that, so I tried usingQScreen
in the following way:const auto screens = QApplication::screens(); for (const auto& screen : screens) { const auto geometry = screen->geometry(); const auto width = geometry.width(); const auto height = geometry.height(); }
However, when I try this, it appears that
screens
has size 0, so no screens are found...Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than
QScreen
? -
I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:
Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the
QDesktopWidget
class is obsolete, I want to avoid using that, so I tried usingQScreen
in the following way:const auto screens = QApplication::screens(); for (const auto& screen : screens) { const auto geometry = screen->geometry(); const auto width = geometry.width(); const auto height = geometry.height(); }
However, when I try this, it appears that
screens
has size 0, so no screens are found...Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than
QScreen
?@Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:
it appears
it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.
-
I'm a bit confused... Using Qt 5.15.1, I would like to retrieve the "Display resolution" of my laptop (no external screens attached), as given in Windows 10 as follows:
Since according to https://doc.qt.io/qt-5/qdesktopwidget.html the
QDesktopWidget
class is obsolete, I want to avoid using that, so I tried usingQScreen
in the following way:const auto screens = QApplication::screens(); for (const auto& screen : screens) { const auto geometry = screen->geometry(); const auto width = geometry.width(); const auto height = geometry.height(); }
However, when I try this, it appears that
screens
has size 0, so no screens are found...Am I on the right track to retrieve my Display resolution, or am I doing stuff completely wrong and should I use something else than
QScreen
?@Bart_Vandewoestyne works perfectly fine for me, it it is even from inside a VM
-
@Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:
it appears
it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.
@jsulm said in How to retrieve laptop's 'Display resolution'?:
it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.
I did check it as part of a larger application and it was really 0. However, I now also tried the following trimmed down minimal example:
#include <QApplication> #include <QScreen> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); const auto screens = QApplication::screens(); qDebug() << screens.size(); for (const auto& screen : screens) { const auto geometry = screen->geometry(); const auto screenName = screen->name(); const auto width = geometry.width(); const auto height = geometry.height(); qDebug() << width << height; } }
and .pro file
TEMPLATE = app TARGET = qscreen INCLUDEPATH += . QT += widgets CONFIG += console SOURCES += qscreen.cpp
and that works. So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)
-
@jsulm said in How to retrieve laptop's 'Display resolution'?:
it appears or did you check it is really 0? Because inside the loop you're defining local variables without doing anything with them.
I did check it as part of a larger application and it was really 0. However, I now also tried the following trimmed down minimal example:
#include <QApplication> #include <QScreen> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); const auto screens = QApplication::screens(); qDebug() << screens.size(); for (const auto& screen : screens) { const auto geometry = screen->geometry(); const auto screenName = screen->name(); const auto width = geometry.width(); const auto height = geometry.height(); qDebug() << width << height; } }
and .pro file
TEMPLATE = app TARGET = qscreen INCLUDEPATH += . QT += widgets CONFIG += console SOURCES += qscreen.cpp
and that works. So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)
@Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:
So I'll now have to figure out what differences there are between this trimmed down program and my larger application. Stay tuned! I'll report back later! :-)
I'm a bit confused... as I now have:
- A trimmed down stand-alone example program that works.
- A unit test using the Google Test framework that I got working after creating a new Google Test
main()
function with
right beforeQApplication a(argc, argv);
return RUN_ALL_TESTS();
- In my larger application, I have main thread where, the first statement in my
main()
function is
Next to the main thread, I have another thread with an event loop, and it is in that thread that I'm trying to fetch the screen info, but it fails.QCoreApplication app(argc, argv);
Any idea what could be wrong here? I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program, and the second thread does have an event loop (although I'm not sure if that is relevant for fetching the screen info)...
-
@Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:
I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program
QCoreApplication
is the base class for non-UI applications.
For UI stuff (desktop included) you need at leastQGuiApplication
orQApplication
for widget based app. -
@Bart_Vandewoestyne said in How to retrieve laptop's 'Display resolution'?:
I'm quite in the dark, since I do call the QCoreApplication constructor in the beginning of my program
QCoreApplication
is the base class for non-UI applications.
For UI stuff (desktop included) you need at leastQGuiApplication
orQApplication
for widget based app.@Chris-Kawa said in How to retrieve laptop's 'Display resolution'?:
QCoreApplication
is the base class for non-UI applications.
For UI stuff (desktop included) you need at leastQGuiApplication
orQApplication
for widget based app.I indeed overlooked that one. After switching from
QCoreApplicatio
toQGuiApplication
everything works as expected and my screen is detected.Thanks!