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 actual widget width and height in constructor?
Qt 6.11 is out! See what's new in the release blog

How to get actual widget width and height in constructor?

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 1.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.
  • Q Offline
    Q Offline
    Qt_Andrew
    wrote on last edited by
    #1

    I want to adjust some items in a qtablewidget when ui satrted.
    And I put code in constructor of mainwindow class, then It didn't work.
    If I put same code in resizeEvent(), it worked.

        for (int r = 0; r < 5r++) {
            for (int c = 0; c < 5c++) {
                ui->TBW->setColumnWidth(c, ui->TBW->width/5);
            }
            ui->TBW->setRowHeight(r, ui->TBW->height/5);
        }
    

    How to get actual widget width and height in constructor?

    Pl45m4P 1 Reply Last reply
    0
    • Q Qt_Andrew

      I want to adjust some items in a qtablewidget when ui satrted.
      And I put code in constructor of mainwindow class, then It didn't work.
      If I put same code in resizeEvent(), it worked.

          for (int r = 0; r < 5r++) {
              for (int c = 0; c < 5c++) {
                  ui->TBW->setColumnWidth(c, ui->TBW->width/5);
              }
              ui->TBW->setRowHeight(r, ui->TBW->height/5);
          }
      

      How to get actual widget width and height in constructor?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Qt_Andrew said in How to get actual widget width and height in constructor?:

      How to get actual widget width and height in constructor?

      You can't, because some events occur after constructing the widget, so the layouts and some other rules apply. This causes the widgets height and width to be different in constructor and after construction (at runtime after widget is shown).

      What you can do is setting a fixed size to make sure that your widget keeps the size, but then you cannot resize it anymore.

      EDIT:

      If your code works in resizeEvent, what prevents you from keeping it there?
      Instead of using the resizeEvent you could also use the showEvent from your parent widget (QMainWindow I guess), where your QTableWidget is located. Should have the same effect.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      2

      • Login

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