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. Is automatic (re)sizing of QTableWidget possible?

Is automatic (re)sizing of QTableWidget possible?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.8k 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.
  • A Offline
    A Offline
    A1exander_Z
    wrote on last edited by
    #1

    I cannot make QTableWidget automatically size or resize itself. Here is a fragment of code I use:

    @QWidget* w = new QWidget();
    w->setLayout(new QVBoxLayout());
    // ...
    // Here I create another widget called 'plot'
    // ...
    w->layout()->addWidget(plot);
    // Add inner horizontal layout to vertical layout of widget 'w'
    QHBoxLayout* l = new QHBoxLayout();
    static_cast<QVBoxLayout*>(w->layout())->addLayout(l);
    // Prepare the table
    QTableWidget* table = new QTableWidget(4, 6, this);
    table->verticalHeader()->setVisible(false);
    table->horizontalHeader()->setVisible(false);
    // ...
    // here I fill the table (all 6x4 cells)
    // ...
    l->addWidget(table); l->addStretch();
    ui->tabWidget->addTab(w, "Test");
    @

    I want the following. The tab widget (created in designer) has the central widget 'w', which has a vertical outer layout (containing the 'plot' widget and an inner horizontal layout). The inner layout contains QTableWidget 'table' and a spacer. The table should resize itself to occupy minimal, but sufficient space to show all its rows and columns (the tab is large enough to allow this). However, the height of the table is always larger than necessary (there is enough space for a couple of additional rows) and the width is either larger or smaller than necessary (if it is smaller, a horizontal scrollbar appears, although the tab width is much larger than the width of all columns combined).

    Calling table->resizeColumnsToContents() and table->resizeRowsToContents() does not help. Setting size policy of the table (to any size policy) does not help either. Is there a way to automatically (re)size QTableWidget?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      If using spacers the layout of the widget will work with the resizeToColumn functions etc.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • A Offline
        A Offline
        A1exander_Z
        wrote on last edited by
        #3

        What kind of spacers is needed for these functions to work? I already have a stretchable space added to the inner horizontal layout, it does not help. It forces the table to occupy exactly one half of the tab width (or some other fixed amount of space if I set stretching factors manually). And if I add a stretchable space to the outer vertical layout, this leads to shrinking of the plot widget, not the table.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          It is very hard to tell what to do without a picture or layout setup.
          Does your TabWidget tabs contain a layout? Otherwise the tab will just hold a widget (which does have a layout and will resize if the widget is changed) that will not resize with the resize of the tabwidget.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ankursaxena
            wrote on last edited by
            #5

            Just use. Any kind of layout. put table widget in that. It will resize automatically.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              A1exander_Z
              wrote on last edited by
              #6

              All right, I have tried to picture it:
              !http://i62.tinypic.com/r8s5xz.jpg(Layouts)!
              Widget 'w', created at runtime, is inserted as a new tab into tab widget (ui->tabWidget->addTab(w, "Test")). The tab widget itself is inserted into a vertical layout and resizes itself properly. Widget 'w' has a vertical layout with the 'plot' widget and a horizontal layout inserted into it. The plot widget also has no problems with resizing. Horizontal layout has table widget and a stretchable space. The table widget resizes with the window, but its initial size is always larger than its contents (both vertical and horizontal dimensions, if I do not specify stretching factors). I have also tried to make an additional inner layout for the table only:

              @QHBoxLayout* ll = new QHBoxLayout();
              l->addLayout(ll);
              ll->addWidget(table); l->addStretch();@

              It has no effect.

              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