QScreen size returning zero
Hi,
I am using the following function to return screen size:
Code:
screenWidth = screen->availableSize().width();
screenHeight = screen->availableSize().height();
I tested this code by making a push button and on the button click, it runs this code and it works. Although when I try putting this code in my constructor function, it keeps returning screenWidth = 0 and screenHeight = 0.
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
screenWidth = screen->availableSize().width();
screenHeight = screen->availableSize().height();
}
Any help would be greatly appreciated, thank you :).
Re: QScreen size returning zero
From the description I would guess that the determination of the screen size requires some event based interaction between the application and the system.
The constructor of the main window is usually run before the event loop is started, so it might not have had a chance yet to do that.
You could try calling that code delayed using QTimer::singleShot() with a 0 timeout.
Cheers,
_
Re: QScreen size returning zero
Hi, thank you for your help! This is how I got it to work. Also I don't know how to change the title of the thread to [solved] lol
Code:
void mainwindowWing::delay()
{
while (QTime::currentTime() < dieTime
) }
mainwindowWing
::mainwindowWing(QWidget *parent
) : ui(new Ui::mainwindowWing)
{
ui->setupUi(this);
delay();
QTimer::singleShot( 0,
this,
SLOT( setupGUI
()) );
}
Re: QScreen size returning zero
Are you sure you need that "delay" method?
The single shot time will already delay invocation of setupGUI to after the event loop has started processing events.
Cheers,
_
Re: QScreen size returning zero
Yeah I understand although it works with it and does not work without it, haha :p
Re: QScreen size returning zero
Hmm.
Have you tried connecting to the QScreen's availableGeometryChanged() signal as the trigger?
If you need those values that might be a good idea anyway in case the geometry changes again.
Cheers,
_
Re: QScreen size returning zero
It seems likely that the application is not "connected" to a screen until the window is shown. Try looking at the values in the showEvent() handler.
Why do you need the screen geometry before your window is shown?