Results 1 to 7 of 7

Thread: QScreen size returning zero

  1. #1
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default QScreen size returning zero

    Hi,

    I am using the following function to return screen size:

    Qt Code:
    1. QScreen *screen = QApplication::screens().at(0);
    2. screenWidth = screen->availableSize().width();
    3. screenHeight = screen->availableSize().height();
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7. QScreen *screen = QApplication::screens().at(0);
    8. screenWidth = screen->availableSize().width();
    9. screenHeight = screen->availableSize().height();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated, thank you .

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    alistair (4th August 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default 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
    Qt Code:
    1. void mainwindowWing::delay()
    2. {
    3. QTime dieTime= QTime::currentTime().addMSecs(1);
    4. while (QTime::currentTime() < dieTime)
    5. QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
    6. }
    7.  
    8. mainwindowWing::mainwindowWing(QWidget *parent) :
    9. QWidget(parent),
    10. ui(new Ui::mainwindowWing)
    11. {
    12. ui->setupUi(this);
    13. delay();
    14. QTimer::singleShot( 0, this, SLOT( setupGUI()) );
    15. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by alistair; 4th August 2015 at 03:16.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  6. #5
    Join Date
    Jul 2015
    Posts
    11
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: QScreen size returning zero

    Yeah I understand although it works with it and does not work without it, haha

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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?

Similar Threads

  1. Returning from Dialog
    By SSqt5.2 in forum Newbie
    Replies: 9
    Last Post: 20th October 2014, 10:16
  2. QSettings beginReadarray not returning the exact size.
    By chinmayapadhi in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2010, 09:54
  3. Returning const &
    By frenk_castle in forum Newbie
    Replies: 2
    Last Post: 24th November 2009, 07:01
  4. Re-implementing blit() of QScreen class
    By node_ex in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2009, 07:36
  5. QScreen example
    By punit.doshi.85 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 27th September 2008, 04:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.