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 dimensions of the widgets
Qt 6.11 is out! See what's new in the release blog

How to get the dimensions of the widgets

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 2.8k 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.
  • Swati777999S Offline
    Swati777999S Offline
    Swati777999
    wrote on last edited by
    #1

    Hi All,

    I want to know how to get the dimensions of the widgets.

    Through following codes, I get the default dimension of my widget (height=480,width=640)
    QTableWidget *first_table=new QTableWidget();
    tableLayout->addWidget(first_table,0,0);
    int height =first_table->height();
    int width = first_table->width();
    qDebug()<<"height is"<<height;
    qDebug()<<"width is"<<width;

    After I did some operations like adding spacing in horizontal and vertical directions, then used height() and width() to get the dimensions of the widgets, it still gives the default dimensions.

    How to get the exact dimension of any widget?

    Thanks.

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    jsulmJ 1 Reply Last reply
    0
    • Swati777999S Swati777999

      Hi All,

      I want to know how to get the dimensions of the widgets.

      Through following codes, I get the default dimension of my widget (height=480,width=640)
      QTableWidget *first_table=new QTableWidget();
      tableLayout->addWidget(first_table,0,0);
      int height =first_table->height();
      int width = first_table->width();
      qDebug()<<"height is"<<height;
      qDebug()<<"width is"<<width;

      After I did some operations like adding spacing in horizontal and vertical directions, then used height() and width() to get the dimensions of the widgets, it still gives the default dimensions.

      How to get the exact dimension of any widget?

      Thanks.

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

      @Swati777999 said in How to get the dimensions of the widgets:

      it still gives the default dimensions.

      Where do you read the dimensions? If you do it in constructor before showing the parent widget you will not get correct geometry. The reason is that widgets are sized properly when they are shown, not before.

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

      Swati777999S 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Swati777999 said in How to get the dimensions of the widgets:

        it still gives the default dimensions.

        Where do you read the dimensions? If you do it in constructor before showing the parent widget you will not get correct geometry. The reason is that widgets are sized properly when they are shown, not before.

        Swati777999S Offline
        Swati777999S Offline
        Swati777999
        wrote on last edited by
        #3

        @jsulm
        Default dimension means height=480 and width=640

        No matter how many manipulations I do to the widgets through different operations, the size I get is height=480 and width=640 which is same as that of the main window.

        “ In order to be irreplaceable, one must always be different” – Coco Chanel

        jsulmJ JonBJ 2 Replies Last reply
        0
        • Swati777999S Swati777999

          @jsulm
          Default dimension means height=480 and width=640

          No matter how many manipulations I do to the widgets through different operations, the size I get is height=480 and width=640 which is same as that of the main window.

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

          @Swati777999 Are your widgets in a layout?
          Are you doing these manipulations in constructor?

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

          Swati777999S 1 Reply Last reply
          0
          • Swati777999S Swati777999

            @jsulm
            Default dimension means height=480 and width=640

            No matter how many manipulations I do to the widgets through different operations, the size I get is height=480 and width=640 which is same as that of the main window.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Swati777999
            @jsulm already told you:

            Where do you read the dimensions? If you do it in constructor before showing the parent widget you will not get correct geometry. The reason is that widgets are sized properly when they are shown, not before.

            This is the correct answer. Please take the time to consider people's answers and act on them.

            Again: widgets do not report any reliable size until they shown, as Qt does not do the calculations till it needs to as & when it displays them.

            1 Reply Last reply
            0
            • jsulmJ jsulm

              @Swati777999 Are your widgets in a layout?
              Are you doing these manipulations in constructor?

              Swati777999S Offline
              Swati777999S Offline
              Swati777999
              wrote on last edited by
              #6

              @jsulm

              Yes, my widgets are in a GridLayout.
              No, I am not doing these manipulations in the constructor.

              “ In order to be irreplaceable, one must always be different” – Coco Chanel

              JonBJ 1 Reply Last reply
              0
              • Swati777999S Swati777999

                @jsulm

                Yes, my widgets are in a GridLayout.
                No, I am not doing these manipulations in the constructor.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @Swati777999 said in How to get the dimensions of the widgets:

                No, I am not doing these manipulations in the constructor.

                OK, then tell us where you are looking at these sizes, and how it relates to the shown state of the widgets!

                Swati777999S 1 Reply Last reply
                1
                • JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by
                  #8

                  override showEvent func and check sizes of widgets inside this func. The layout has not started where you check size of the widgets.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Swati777999 said in How to get the dimensions of the widgets:

                    No, I am not doing these manipulations in the constructor.

                    OK, then tell us where you are looking at these sizes, and how it relates to the shown state of the widgets!

                    Swati777999S Offline
                    Swati777999S Offline
                    Swati777999
                    wrote on last edited by
                    #9

                    @JonB

                    I've performed the following manipulations to change the size

                    first_table->setMinimumSize(100,100);
                    first_table->setRowCount(8);
                    first_table->setColumnCount(2);
                    int height =first_table->height();
                    int width = first_table->width();
                    qDebug()<<"height is"<<height;
                    qDebug()<<"width is"<<width;
                    

                    It gives the result as height=480 ,width=640

                    “ In order to be irreplaceable, one must always be different” – Coco Chanel

                    JKSHJ 1 Reply Last reply
                    0
                    • Swati777999S Swati777999

                      @JonB

                      I've performed the following manipulations to change the size

                      first_table->setMinimumSize(100,100);
                      first_table->setRowCount(8);
                      first_table->setColumnCount(2);
                      int height =first_table->height();
                      int width = first_table->width();
                      qDebug()<<"height is"<<height;
                      qDebug()<<"width is"<<width;
                      

                      It gives the result as height=480 ,width=640

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @Swati777999 said in How to get the dimensions of the widgets:

                      first_table->setMinimumSize(100,100);
                      first_table->setRowCount(8);
                      first_table->setColumnCount(2);
                                           // <-- ERROR HERE! You did not wait until the changes have been applied.
                                           //                 Therefore, the lines below will give you wrong numbers.
                      int height =first_table->height();
                      int width = first_table->width();
                      

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      Swati777999S 1 Reply Last reply
                      2
                      • JKSHJ JKSH

                        @Swati777999 said in How to get the dimensions of the widgets:

                        first_table->setMinimumSize(100,100);
                        first_table->setRowCount(8);
                        first_table->setColumnCount(2);
                                             // <-- ERROR HERE! You did not wait until the changes have been applied.
                                             //                 Therefore, the lines below will give you wrong numbers.
                        int height =first_table->height();
                        int width = first_table->width();
                        
                        Swati777999S Offline
                        Swati777999S Offline
                        Swati777999
                        wrote on last edited by
                        #11

                        @JKSH

                        How can I introduce the time for waiting after manipulating the dimension of widget?

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        JKSHJ 1 Reply Last reply
                        0
                        • Swati777999S Swati777999

                          @JKSH

                          How can I introduce the time for waiting after manipulating the dimension of widget?

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by
                          #12

                          @Swati777999 said in How to get the dimensions of the widgets:

                          How can I introduce the time for waiting after manipulating the dimension of widget?

                          Have you tried @JoeCFD's suggestion?

                          You cannot call width()/height() in the same function that calls manipulators like first_table->setMinimumSize(100,100). The changes will not be applied until all of your functions have returned -- that's when your application's Event Loop can process the changes.

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          2
                          • Ketan__Patel__0011K Offline
                            Ketan__Patel__0011K Offline
                            Ketan__Patel__0011
                            wrote on last edited by Ketan__Patel__0011
                            #13

                            You can try this

                            ui->YourWidget->geometry();

                            it's Return Your Widget Location And Size;

                            JKSHJ 1 Reply Last reply
                            0
                            • Ketan__Patel__0011K Ketan__Patel__0011

                              You can try this

                              ui->YourWidget->geometry();

                              it's Return Your Widget Location And Size;

                              JKSHJ Offline
                              JKSHJ Offline
                              JKSH
                              Moderators
                              wrote on last edited by
                              #14

                              @Ketan__Patel__0011 said in How to get the dimensions of the widgets:

                              You can try this

                              ui->YourWidget->geometry();

                              it's Return Your Widget Location And Size;

                              This won't work before the widget is shown.

                              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                              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