Get screensize
-
hello,
I'm trying to get the screen size for my desktop app.
project file:
QT += core gui
headers
#include <qrect.h> #include <qscreen.h> #include <QMessageBox>
QScreen *screen = QGuiApplication::primaryScreen(); QRect screenGeometry = screen->geometry(); int height = screenGeometry.height(); int width = screenGeometry.width(); QMessageBox msgBox; msgBox.setText((QString)width + " x " + (QString)height); msgBox.exec();
or
QSize size = qApp->screens()[0]->size(); QMessageBox msgBox; msgBox.setText(((QString)size.width()+ " x " + (QString)size.height() ); msgBox.exec();
but both methods return "garbage", what am i missing?
regards
-
@Natural_Bugger said in Get screensize:
msgBox.setText((QString)width + " x " + (QString)height);
You should use
QString::number()
instead:msgBox.setText(QString::number(width) + " x " + QString::number(height));
-
thnx,
it works perfect for the first method.
but not for the second.QSize size = qApp->screens()[0]->size(); QMessageBox msgBox; msgBox.setText(QString::number(size.width()) + " x " + QString::number(size.height())); msgBox.exec();
it returns:
0801 x 0291
when it should return 1920 x 1080
-
@Natural_Bugger
Well the code looks right to me now. Just in case, what doesqApp->screens().count()
return ?I'm "surprised" at one thing. Your output
0801
&0291
have leading0
s, and are 4 digits long. That does not look like the default output fromQString::number()
, what is going on?EDIT Oh, I see what @sierdzio is saying below. If you read them right-to-left then you do get
1920 x 1080
. I think somewhere you have indeed set for Right-to-Left text output! Change your string" x "
over to" <<< "
and see which direction those chevrons come out --- is it indeed>>>
?! EDIT My bad. Corrected in my post below. -
Woah, that's bizarre!
The only thing that comes to my mind is an RTL language, but even that should not change number direction.
-
thnx @JonB
QMessageBox msgBox; msgBox.setText(QString::number(qApp->screens().count())); msgBox.exec();
returns 1
but when i change ...
QSize size = qApp->screens()[0]->size();
to
QSize size = qApp->screens()[1]->size();
it doesn't compile
-
@Natural_Bugger said in Get screensize:
QSize size = qApp->screens()[1]->size();
Anyway, I now see you do indeed only have one screen. Just forget about this area.
In any case, I have edited my answer. Somewhere I really think you have a text right-to-left issue! Do what I have edited above so that the string has literal
<<<
in it, which direction do those come out? -
Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.
Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):
QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
-
@JonB said in Get screensize:
I think somewhere you have indeed set for Right-to-Left text output! Change your string" x "
over to" <<< "
and see which direction those chevrons come out --- is it indeed>>>
?!thank you for your answer.
chevrons?
what do i need to do?
-
@Natural_Bugger said in Get screensize:
what do i need to do?
I told you. Change your string
" x "
to" <<< "
, how much clearer can I be?msgBox.setText(QString::number(size.width()) + " <<< " + QString::number(size.height()));
EDIT I'm sorry, my bad, I've just realized I was thinking too literally, like a mirror!
Try:
msgBox.setText(QString::number(size.width()) + " abc " + QString::number(size.height()));
I want to know whether that comes out as
abc
or ascba
? -
@sierdzio said in Get screensize:
Yeah I'm right but I shouldn't be :D RTL should not invert numbers, as far as I know, only regular text.
Perhaps this will work better (https://doc.qt.io/qt-5/qlocale.html#toString-6):
QLocale::toString(size.width()) + " x " + QLocale::toStringsize.height())
#include <QLocale>
returns
error: cannot call member function ‘QString QLocale::toString(int) const’ without object ^
-
@Natural_Bugger
Yup! So we are all agreed: there is right-to-left going on here, the results of the screen size are correct but are being printed right-to-left! Not my area, but that is what needs sorting out.... -
@Natural_Bugger ok it's not a static method, eh.
Then:
QLocale locale; msgBox.setText(locale.toString(size.width()) + " x " + locale.toStringsize.height()));
-
-
@Natural_Bugger
Using a custom button which callsreverse
is not a good place to start from....But I don't understand, the code you gave us uses
QMessageBox::setText()
, which is what we looked at, what has that got to do with any custom button, or thereverseText()
method? -
Hah, ok good that you've found it. Happy coding!