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. How to detect when QWidget’s layout is fully updated and geometry is ready after dynamic content changes?

How to detect when QWidget’s layout is fully updated and geometry is ready after dynamic content changes?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 290 Views
  • 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.
  • K Offline
    K Offline
    Kevin Hoang
    wrote on 7 Apr 2025, 17:02 last edited by
    #1

    Hi everyone,

    I'm working on a dynamic UI component where I dynamically add child widgets (e.g., QLabel) into a QBoxLayout (such as QVBoxLayout).

    Here is my situation:

    • Initially, the main QWidget has size (0, 0) because there’s no content yet.
    • When I add an item (like a QLabel) to the layout, I expect the widget's size to automatically adjust to fit the content.
    • However, immediately after adding the item, the parent widget's geometry and size are still (0, 0), and sizeHint() is (0, 0).
    • The geometry (and correct size) updates only later, but there's no clear event to detect exactly when the layout is finished updating.

    My need:

    • I need to know exactly when the widget has updated its layout and geometry properly so I can calculate a proper display position.
    • I also need the geometry ready before starting any QPropertyAnimation on the widget.
    • Currently, if I trigger animation or positioning immediately after adding the item, the widget hasn’t resized yet, and it causes incorrect results.

    What I’ve tried:

    • adjustSize()
    • layout()->invalidate()
    • QTimer::singleShot(0, ...)
    • ...

    None of these methods consistently work: the widget’s size is still not reliable right after these calls.

    My Question:
    👉 How can I reliably detect when a QWidget's layout has fully recalculated, the size and geometry are correct, and the widget is ready for positioning and animation?

    Any advice, tips, or workaround would be greatly appreciated!

    J J 2 Replies Last reply 7 Apr 2025, 17:12
    0
    • K Kevin Hoang
      7 Apr 2025, 17:02

      Hi everyone,

      I'm working on a dynamic UI component where I dynamically add child widgets (e.g., QLabel) into a QBoxLayout (such as QVBoxLayout).

      Here is my situation:

      • Initially, the main QWidget has size (0, 0) because there’s no content yet.
      • When I add an item (like a QLabel) to the layout, I expect the widget's size to automatically adjust to fit the content.
      • However, immediately after adding the item, the parent widget's geometry and size are still (0, 0), and sizeHint() is (0, 0).
      • The geometry (and correct size) updates only later, but there's no clear event to detect exactly when the layout is finished updating.

      My need:

      • I need to know exactly when the widget has updated its layout and geometry properly so I can calculate a proper display position.
      • I also need the geometry ready before starting any QPropertyAnimation on the widget.
      • Currently, if I trigger animation or positioning immediately after adding the item, the widget hasn’t resized yet, and it causes incorrect results.

      What I’ve tried:

      • adjustSize()
      • layout()->invalidate()
      • QTimer::singleShot(0, ...)
      • ...

      None of these methods consistently work: the widget’s size is still not reliable right after these calls.

      My Question:
      👉 How can I reliably detect when a QWidget's layout has fully recalculated, the size and geometry are correct, and the widget is ready for positioning and animation?

      Any advice, tips, or workaround would be greatly appreciated!

      J Offline
      J Offline
      JonB
      wrote on 7 Apr 2025, 17:12 last edited by JonB 4 Jul 2025, 17:15
      #2

      @Kevin-Hoang
      I don't know the answer to your complicated situation. But I think you might read through

      • https://stackoverflow.com/questions/17307867/qt-when-is-layout-final
      • https://stackoverflow.com/questions/78795785/is-there-a-qt-event-for-when-a-qlayout-has-finished-redoing-geometry

      Also, you said QTimer::singleShot(0, ...) did not work. What about QTimer::singleShot(1000, ...), if that does not work don't you have a problem? I am not suggesting this for long term, but as a sanity check.

      Btw, I'm not sure it's what you want but every widget has a void QWidget::showEvent(QShowEvent *event), you can use that if you have sibclassed, check that for correct size if you want that?

      1 Reply Last reply
      0
      • K Kevin Hoang
        7 Apr 2025, 17:02

        Hi everyone,

        I'm working on a dynamic UI component where I dynamically add child widgets (e.g., QLabel) into a QBoxLayout (such as QVBoxLayout).

        Here is my situation:

        • Initially, the main QWidget has size (0, 0) because there’s no content yet.
        • When I add an item (like a QLabel) to the layout, I expect the widget's size to automatically adjust to fit the content.
        • However, immediately after adding the item, the parent widget's geometry and size are still (0, 0), and sizeHint() is (0, 0).
        • The geometry (and correct size) updates only later, but there's no clear event to detect exactly when the layout is finished updating.

        My need:

        • I need to know exactly when the widget has updated its layout and geometry properly so I can calculate a proper display position.
        • I also need the geometry ready before starting any QPropertyAnimation on the widget.
        • Currently, if I trigger animation or positioning immediately after adding the item, the widget hasn’t resized yet, and it causes incorrect results.

        What I’ve tried:

        • adjustSize()
        • layout()->invalidate()
        • QTimer::singleShot(0, ...)
        • ...

        None of these methods consistently work: the widget’s size is still not reliable right after these calls.

        My Question:
        👉 How can I reliably detect when a QWidget's layout has fully recalculated, the size and geometry are correct, and the widget is ready for positioning and animation?

        Any advice, tips, or workaround would be greatly appreciated!

        J Offline
        J Offline
        JoeCFD
        wrote on 7 Apr 2025, 17:33 last edited by
        #3

        @Kevin-Hoang
        if the main widget changes size, resize event may be the better choice. Override the following event in your main widget class.
        void resizeEvent(QResizeEvent *event) override;

        1 Reply Last reply
        1
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Apr 2025, 17:35 last edited by
          #4

          Hi,

          This will possible once the widget is shown before that it does not have any "physical" presence.
          As @JoeCFD wrote, resizeEvent is called when widgets have changed size.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • K Offline
            K Offline
            Kevin Hoang
            wrote on 8 Apr 2025, 08:32 last edited by
            #5

            Thank you all for your support!
            Using the resize event works well for my case. It allows me to get the correct geometry as I expected.
            There is still a minor issue, but it’s not a big concern for my application.
            Thanks again for your helpful suggestions!

            1 Reply Last reply
            1
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 Apr 2025, 20:08 last edited by
              #6

              You're welcome !
              Since you have it working now, please mark the thread as solved using the "Topic Tools" button or the three dotted menu beside the answer you deem correct so other forum users may know a solution has been found :-)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • K Kevin Hoang has marked this topic as solved on 9 Apr 2025, 12:54

              1/6

              7 Apr 2025, 17:02

              • Login

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