Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get QDialog size before showing it
Forum Updated to NodeBB v4.3 + New Features

Get QDialog size before showing it

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 4.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JulienMaille
    wrote on last edited by
    #1

    Dear Qt users,
    I'm trying to position a QDialog according to user request (centered on screen, in a corner, etc) and I need to retrieve its width and height before actually showing it.

    I found someone with the same problem and he ended up with a dirty trick that involves some processEvents() that I really don't want to have in my code.

    http://stackoverflow.com/questions/5417201/qt-place-new-window-correctly-on-screen-center-over-mouse-move-into-screen
    @// BIG PAIN: We want to get the dialog box to caluclate its own size. But there is
    // no simple way to do this. The following seems to work, but only if processEvents
    // is called at least twice. God knows why:
    setAttribute (Qt::WA_DontShowOnScreen, true) ; // Prevent screen flicker
    show() ;

    QEventLoop EventLoop (this) ;
    for (int i = 0 ; i < 10 ; i++)
    if (!EventLoop.processEvents()) break ;

    hide() ;
    setAttribute (Qt::WA_DontShowOnScreen, false) ;@

    From what I read:
    [quote]From the code I've looked at and the Qt defect reports I know why you have to call processEvents: there is a lazy size evaluation, the code to produce a size is only called in response to certain events, like show and resize. I looked through the code and although the sizing functions do exist, there is no possibility to call them directly[/quote]
    Is that sill true?

    1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      according to the "docs of QWidget::updateGeometry()":http://qt-project.org/doc/qt-4.8/qwidget.html#updateGeometry i believe it's true.

      But why do you need to know the size beforehand? Can't you just listen to the resize event?
      If you just need to center it on the screen, do this in the showEvent() handler:
      @
      void MyDialog::showEvent(QEvent* event)
      {
      QDialog::showEvent(event);
      //do centering here, size should be already correct at this point
      }
      @

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JulienMaille
        wrote on last edited by
        #3

        Hum, I did not try to subclass QDialog yet. If this is my only option, I will try that.

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          you can also go with an eventFilter on the dialog. When the showEvent comes in you will have to call the centering of the dialog delayed.
          E.g. with a singleshot timer:
          @
          QTimer::singleShot(0, this, SLOT(centerDialogOnScreen()));
          @

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mstanescu
            wrote on last edited by
            #5

            Hi all,

            I need this too but none of the exposed solutions actually work.

            bq. If you just need to center it on the screen, do this in the showEvent() handler

            I tried this and does not work in release build with qt 4.8.5. It does work on debug builds. Don't ask me why. :-(

            bq. QTimer::singleShot(0, this, SLOT(centerDialogOnScreen()));

            If you emit delayed then you will have a slight flickering (i already did this). This works in debug and release builds

            Does anyone know why the synchronous call does not work? I tried to look in Qt source code but not without a lot of success

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved