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 get the real size of the widget? the result return by the size() & geometry() is not the real size when app runs

how to get the real size of the widget? the result return by the size() & geometry() is not the real size when app runs

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 7 Posters 1.0k Views 2 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.
  • O Offline
    O Offline
    opengpu
    wrote on last edited by
    #1

    how to get the real size of the widget? the result return by the size() & geometry() is not the real size when app runs

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      size() & geometry() do report the real size
      however , if you ask about sizes in the constructor
      they might not be updated yet as some calculations
      are cached until the showEvent to allow layout etc to
      be up and running.

      1 Reply Last reply
      2
      • O Offline
        O Offline
        opengpu
        wrote on last edited by
        #3

        yep, so how can i get the showed size in the constructor? what is the best method.
        thanks

        mrjjM J.HilkJ jsulmJ 3 Replies Last reply
        0
        • O opengpu

          yep, so how can i get the showed size in the constructor? what is the best method.
          thanks

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @opengpu
          Well you cant.
          What do you need it for ?
          Maybe there some other way to do what you want.

          1 Reply Last reply
          1
          • O opengpu

            yep, so how can i get the showed size in the constructor? what is the best method.
            thanks

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @opengpu you can check the size after the window is shown.

            protected:
                void showEvent(QShowEvent *event) override{
                    Q_UNUSED(event);
                    qDebug() << "Geometry after showEvent" << geometry();
               }
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            1 Reply Last reply
            2
            • O opengpu

              yep, so how can i get the showed size in the constructor? what is the best method.
              thanks

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @opengpu said in how to get the real size of the widget? the result return by the size() & geometry() is not the real size when app runs:

              so how can i get the showed size in the constructor?

              While constructor is being executed the widget is not yet shown, so it is simply impossible.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              5
              • dheerendraD Offline
                dheerendraD Offline
                dheerendra
                Qt Champions 2022
                wrote on last edited by
                #7

                When app runs ?

                What do you mean by this ? It helps if you show where are you trying to get the size ? You can use something like rect() also to get the size. As everybody said, you can't get the size unless it is shown.

                Dheerendra
                @Community Service
                Certified Qt Specialist
                http://www.pthinks.com

                1 Reply Last reply
                1
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #8

                  It's possible to know the size of a widget without showing it, like that:

                  setAttribute(Qt::WA_DontShowOnScreen);
                  show();
                  

                  The issue is if then after you turn off this attribute, the show() method does nothing.
                  It's my experience at least on MacOS.

                  JonBJ 1 Reply Last reply
                  2
                  • M mpergand

                    It's possible to know the size of a widget without showing it, like that:

                    setAttribute(Qt::WA_DontShowOnScreen);
                    show();
                    

                    The issue is if then after you turn off this attribute, the show() method does nothing.
                    It's my experience at least on MacOS.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @mpergand
                    Your suggestion is interesting. However, if the OP wants to go down this route I think he should look carefully at the comments in response to the "accepted solution" at https://stackoverflow.com/a/3996525/489865 (which is where I presume you got it from). They may also explain why you are seeing "odd behaviour" as per your "The issue is if then after you turn off this attribute, the show() method does nothing.". In the same thread https://stackoverflow.com/a/19235153/489865 appears to offer a more complex but "safer" mechanism for determining the size, I believe.

                    1 Reply Last reply
                    1

                    • Login

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