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. Automatically stretch QTableWidget to fit resized window
Forum Updated to NodeBB v4.3 + New Features

Automatically stretch QTableWidget to fit resized window

Scheduled Pinned Locked Moved Solved General and Desktop
layout
18 Posts 7 Posters 45.5k Views 3 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.
  • S Offline
    S Offline
    skebanga
    wrote on 10 Dec 2015, 21:46 last edited by skebanga 12 Oct 2015, 22:08
    #1

    I have a QBoxLayout in QBoxLayout::TopToBottom alignment mode to which I have added 2 QFrame widgets.

    QBoxLayout layout;
    central_widget.setLayout(&layout);
    ...
    layout.addWidget(&form_frame, 0, Qt::AlignTop);
    layout.addWidget(&btn_frame,  0, Qt::AlignBottom);
    

    I would like the form_frame to stretch to fit both vertically and horizontally when the window is resized, but at the moment it only stretches horizontally.

    Here is an example:

    (full working code below)

    The window as it comes up by default:

    default

    The window after resizing:

    resized

    I would like the QTableWidget (or it's parent QFrame) it stretch vertically too, as marked here:

    stretch vertically

    Code producing example:

    #include <QApplication>
    #include <QMainWindow>
    #include <QFormLayout>
    #include <QPushButton>
    #include <QTableWidget>
    
    class App
    {
    public:
        App(int argc, char** argv)
            : app(argc, argv)
            , layout(QBoxLayout::TopToBottom)
        {
            window.setCentralWidget(&central);
        }
    
        int exec()
        {
            central.setLayout(&layout);
            layout.addWidget(&form_frame, 0, Qt::AlignTop);
            layout.addWidget(&btn_frame,  0, Qt::AlignBottom);
    
            form_frame.setLayout(&form_layout);
            btn_frame.setLayout(&btn_layout);
    
            // create a table widget
            QTableWidget* table = new QTableWidget(0, 2);
            form_layout.addRow(table);
            QStringList labels;
            labels << "header 1" << "header 2";
            table->setHorizontalHeaderLabels(labels);
    
            // create some buttons
            btn_save   = new QPushButton("&save");
            btn_cancel = new QPushButton("&cancel");
            btn_layout.addWidget(btn_save);
            btn_layout.addWidget(btn_cancel);
    
            window.show();
            return app.exec();
        }
    
        QApplication app;
        QMainWindow  window;
    
        QWidget      central;
        QBoxLayout   layout;
    
        QFrame       form_frame;
        QFormLayout  form_layout;
    
        QFrame       btn_frame;
        QHBoxLayout  btn_layout;
        QPushButton* btn_save;
        QPushButton* btn_cancel;
    };
    
    int main(int argc, char** argv)
    {
        return App(argc, argv).exec();
    }
    
    
    D 1 Reply Last reply 11 Dec 2015, 04:31
    0
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 10 Dec 2015, 22:05 last edited by
      #2

      Hello,
      Try this layout.setStretch(0, 1);

      Read and abide by the Qt Code of Conduct

      S 1 Reply Last reply 10 Dec 2015, 22:06
      0
      • K kshegunov
        10 Dec 2015, 22:05

        Hello,
        Try this layout.setStretch(0, 1);

        S Offline
        S Offline
        skebanga
        wrote on 10 Dec 2015, 22:06 last edited by
        #3

        @kshegunov said:

        layout.setStretch(0, 1);

        Thanks for the suggestion, but doesn't look like that works

        K 1 Reply Last reply 10 Dec 2015, 22:13
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 10 Dec 2015, 22:08 last edited by
          #4

          Hi,

          Here's a more lightweight version of your UI that does what you want:

          int main(int argc, char** argv)
          {
              QApplication app(argc, argv);
              QMainWindow  window;
              QWidget *centralWidget = new QWidget;
              window.setCentralWidget(centralWidget);
              QVBoxLayout *centralLayout = new QVBoxLayout(centralWidget);
          
              QTableWidget* table = new QTableWidget(0, 2);
              QStringList labels;
              labels << "header 1" << "header 2";
              table->setHorizontalHeaderLabels(labels);
          
              QHBoxLayout *btnLayout = new QHBoxLayout;
              btnLayout->addWidget(new QPushButton("&save"));
              btnLayout->addWidget(new QPushButton("&cancel"));
          
              centralLayout->addWidget(table);
              centralLayout->addLayout(btnLayout);
          
              window.show();
              return app.exec();
          }
          

          It uses less widgets and more suitable layouts.

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

          S 1 Reply Last reply 10 Dec 2015, 23:01
          1
          • S skebanga
            10 Dec 2015, 22:06

            @kshegunov said:

            layout.setStretch(0, 1);

            Thanks for the suggestion, but doesn't look like that works

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 10 Dec 2015, 22:13 last edited by kshegunov 12 Oct 2015, 22:14
            #5

            @skebanga
            Oh, I didn't realize you use a box layout and not a vbox ... silly me.
            Try what @SGaist suggested.
            Also as a note, you can even forgo creating a new central widget:
            QVBoxLayout *centralLayout = new QVBoxLayout(window.centralWidget());
            should do the trick.

            Read and abide by the Qt Code of Conduct

            S 1 Reply Last reply 10 Dec 2015, 22:39
            0
            • K kshegunov
              10 Dec 2015, 22:13

              @skebanga
              Oh, I didn't realize you use a box layout and not a vbox ... silly me.
              Try what @SGaist suggested.
              Also as a note, you can even forgo creating a new central widget:
              QVBoxLayout *centralLayout = new QVBoxLayout(window.centralWidget());
              should do the trick.

              S Offline
              S Offline
              skebanga
              wrote on 10 Dec 2015, 22:39 last edited by
              #6

              @kshegunov

              QVBoxLayout *centralLayout = new QVBoxLayout(window.centralWidget());
              

              That doesn't seem to work - it just gives me a blank screen.

              Seems I have to set a central widget on the QMainWindow in order for it to show anything

              QMainWindow  window;
              QWidget      central;
              window.setCentralWidget(&central);
              QVBoxLayout* centralLayout = new QVBoxLayout(&central);
              
              K 1 Reply Last reply 10 Dec 2015, 22:46
              0
              • S skebanga
                10 Dec 2015, 22:39

                @kshegunov

                QVBoxLayout *centralLayout = new QVBoxLayout(window.centralWidget());
                

                That doesn't seem to work - it just gives me a blank screen.

                Seems I have to set a central widget on the QMainWindow in order for it to show anything

                QMainWindow  window;
                QWidget      central;
                window.setCentralWidget(&central);
                QVBoxLayout* centralLayout = new QVBoxLayout(&central);
                
                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 10 Dec 2015, 22:46 last edited by
                #7

                @skebanga
                It's not my day today. Indeed you do need to create it, I rely on the form editor too much and am losing touch with coded layouts/windows it seems.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                0
                • S SGaist
                  10 Dec 2015, 22:08

                  Hi,

                  Here's a more lightweight version of your UI that does what you want:

                  int main(int argc, char** argv)
                  {
                      QApplication app(argc, argv);
                      QMainWindow  window;
                      QWidget *centralWidget = new QWidget;
                      window.setCentralWidget(centralWidget);
                      QVBoxLayout *centralLayout = new QVBoxLayout(centralWidget);
                  
                      QTableWidget* table = new QTableWidget(0, 2);
                      QStringList labels;
                      labels << "header 1" << "header 2";
                      table->setHorizontalHeaderLabels(labels);
                  
                      QHBoxLayout *btnLayout = new QHBoxLayout;
                      btnLayout->addWidget(new QPushButton("&save"));
                      btnLayout->addWidget(new QPushButton("&cancel"));
                  
                      centralLayout->addWidget(table);
                      centralLayout->addLayout(btnLayout);
                  
                      window.show();
                      return app.exec();
                  }
                  

                  It uses less widgets and more suitable layouts.

                  S Offline
                  S Offline
                  skebanga
                  wrote on 10 Dec 2015, 23:01 last edited by skebanga 12 Oct 2015, 23:01
                  #8

                  @SGaist

                  Thanks for the suggestion.

                  In terms of the layouts and widgets I used in my example, it was a stripped down version of something I have already in my app framework - stripped down to try make a MCVE.

                  What I actually have is something like a building block which I use to create custom "settings dialogs" for various different applications.

                  I have a central window with a QFormLayout at the top and a QHBoxLayout at the bottom with save and cancel QPushButtons.

                  The QFrames have QFrame::Panel frame style to delineate them.

                  The QFormLayout at the top is empty, and the various users add rows to it as they see fit.

                  It sort of looks like this with nothing on it:

                  Empty settings dialog

                  When the user wants to add a new row, they call the following function which gives them back a QFormLayout to which they can add whatever widgets they want:

                  QFormLayout* SettingsDialog::addFormGroup(const QString& title)
                  {
                      QFrame* frame = new QFrame;
                      frame->setFrameStyle(QFrame::Panel);
                      _form_layout.addRow(frame);
                  
                      QFormLayout* form_group_layout = new QFormLayout;
                      frame->setLayout(form_group_layout);
                  
                      if (!title.isEmpty())
                          form_group_layout->addRow(new QLabel(title));
                      return form_group_layout;
                  }
                  
                  

                  Here is an example of it being used with 2 form group rows in the QFormLayout

                  Settings with 2 rows

                  Here is me playing with getting the QTableWidget into my settings dialog:

                  Settings with QTableWidget

                  Here is the same QTableWidget settings dialog when resized, which I am struggling with:

                  Settings with QTableWidget not stretched

                  Sorry if this is getting a bit verbose. Hopefully I've managed to explain what I'm doing and why I have the weird combination of QFrame and QFormLayout etc?

                  Thanks for the assistance!

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 11 Dec 2015, 00:27 last edited by kshegunov 12 Nov 2015, 00:27
                    #9

                    @skebanga
                    Why not just create a simple VBox layout to insert into your central widget layout and add the frames there? Seems the most simple solution to me.

                    Read and abide by the Qt Code of Conduct

                    S 1 Reply Last reply 11 Dec 2015, 15:45
                    1
                    • S skebanga
                      10 Dec 2015, 21:46

                      I have a QBoxLayout in QBoxLayout::TopToBottom alignment mode to which I have added 2 QFrame widgets.

                      QBoxLayout layout;
                      central_widget.setLayout(&layout);
                      ...
                      layout.addWidget(&form_frame, 0, Qt::AlignTop);
                      layout.addWidget(&btn_frame,  0, Qt::AlignBottom);
                      

                      I would like the form_frame to stretch to fit both vertically and horizontally when the window is resized, but at the moment it only stretches horizontally.

                      Here is an example:

                      (full working code below)

                      The window as it comes up by default:

                      default

                      The window after resizing:

                      resized

                      I would like the QTableWidget (or it's parent QFrame) it stretch vertically too, as marked here:

                      stretch vertically

                      Code producing example:

                      #include <QApplication>
                      #include <QMainWindow>
                      #include <QFormLayout>
                      #include <QPushButton>
                      #include <QTableWidget>
                      
                      class App
                      {
                      public:
                          App(int argc, char** argv)
                              : app(argc, argv)
                              , layout(QBoxLayout::TopToBottom)
                          {
                              window.setCentralWidget(&central);
                          }
                      
                          int exec()
                          {
                              central.setLayout(&layout);
                              layout.addWidget(&form_frame, 0, Qt::AlignTop);
                              layout.addWidget(&btn_frame,  0, Qt::AlignBottom);
                      
                              form_frame.setLayout(&form_layout);
                              btn_frame.setLayout(&btn_layout);
                      
                              // create a table widget
                              QTableWidget* table = new QTableWidget(0, 2);
                              form_layout.addRow(table);
                              QStringList labels;
                              labels << "header 1" << "header 2";
                              table->setHorizontalHeaderLabels(labels);
                      
                              // create some buttons
                              btn_save   = new QPushButton("&save");
                              btn_cancel = new QPushButton("&cancel");
                              btn_layout.addWidget(btn_save);
                              btn_layout.addWidget(btn_cancel);
                      
                              window.show();
                              return app.exec();
                          }
                      
                          QApplication app;
                          QMainWindow  window;
                      
                          QWidget      central;
                          QBoxLayout   layout;
                      
                          QFrame       form_frame;
                          QFormLayout  form_layout;
                      
                          QFrame       btn_frame;
                          QHBoxLayout  btn_layout;
                          QPushButton* btn_save;
                          QPushButton* btn_cancel;
                      };
                      
                      int main(int argc, char** argv)
                      {
                          return App(argc, argv).exec();
                      }
                      
                      
                      D Offline
                      D Offline
                      doshirushabh
                      wrote on 11 Dec 2015, 04:31 last edited by
                      #10

                      @skebanga
                      Not sure but please try this. I was facing similar issue , this solved it.
                      SearchTable->horizontalHeader()->setStretchLastSection(true);

                      S 1 Reply Last reply 11 Dec 2015, 15:46
                      1
                      • K kshegunov
                        11 Dec 2015, 00:27

                        @skebanga
                        Why not just create a simple VBox layout to insert into your central widget layout and add the frames there? Seems the most simple solution to me.

                        S Offline
                        S Offline
                        skebanga
                        wrote on 11 Dec 2015, 15:45 last edited by
                        #11

                        Thank you @SGaist and @kshegunov

                        Between your examples and suggestions I have managed to achieve what I want.

                        K 1 Reply Last reply 11 Dec 2015, 19:20
                        0
                        • D doshirushabh
                          11 Dec 2015, 04:31

                          @skebanga
                          Not sure but please try this. I was facing similar issue , this solved it.
                          SearchTable->horizontalHeader()->setStretchLastSection(true);

                          S Offline
                          S Offline
                          skebanga
                          wrote on 11 Dec 2015, 15:46 last edited by
                          #12

                          @doshirushabh

                          SearchTable->horizontalHeader()->setStretchLastSection(true);

                          I don't believe this is what I'm looking for here, as this is to stretch the last section of the table as the table is resized, whereas I want to stretch the table itself.

                          D 1 Reply Last reply 16 Dec 2015, 01:16
                          0
                          • S skebanga
                            11 Dec 2015, 15:45

                            Thank you @SGaist and @kshegunov

                            Between your examples and suggestions I have managed to achieve what I want.

                            K Offline
                            K Offline
                            kshegunov
                            Moderators
                            wrote on 11 Dec 2015, 19:20 last edited by
                            #13

                            @skebanga
                            Good job! :)

                            Read and abide by the Qt Code of Conduct

                            1 Reply Last reply
                            0
                            • S skebanga
                              11 Dec 2015, 15:46

                              @doshirushabh

                              SearchTable->horizontalHeader()->setStretchLastSection(true);

                              I don't believe this is what I'm looking for here, as this is to stretch the last section of the table as the table is resized, whereas I want to stretch the table itself.

                              D Offline
                              D Offline
                              doshirushabh
                              wrote on 16 Dec 2015, 01:16 last edited by
                              #14

                              @skebanga
                              Hello. If you can please tell how did you achieve that . I have similar issues

                              Thanks !!!

                              1 Reply Last reply
                              0
                              • G Offline
                                G Offline
                                GoodPal
                                wrote on 14 Oct 2018, 14:37 last edited by
                                #15

                                In PyQt5 How can this be done?
                                Automatically stretch PyQt5 Widgets to fit resized window?
                                please help me

                                JonBJ 1 Reply Last reply 14 Oct 2018, 14:44
                                0
                                • G GoodPal
                                  14 Oct 2018, 14:37

                                  In PyQt5 How can this be done?
                                  Automatically stretch PyQt5 Widgets to fit resized window?
                                  please help me

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on 14 Oct 2018, 14:44 last edited by
                                  #16

                                  @GoodPal
                                  The fact that you are using PyQt rather than C++ is neither here nor there to the solution. If the code above in the answers works for C++, you just have to translate it to Python/PyQt and it will work there too.

                                  1 Reply Last reply
                                  1
                                  • N Offline
                                    N Offline
                                    nlcodes
                                    wrote on 11 Aug 2020, 17:17 last edited by
                                    #17

                                    So basically, it's not possible. Got it.

                                    S 1 Reply Last reply 11 Aug 2020, 18:17
                                    0
                                    • N nlcodes
                                      11 Aug 2020, 17:17

                                      So basically, it's not possible. Got it.

                                      S Offline
                                      S Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on 11 Aug 2020, 18:17 last edited by
                                      #18

                                      Hi,
                                      @nlcodes said in Automatically stretch QTableWidget to fit resized window:

                                      So basically, it's not possible. Got it.

                                      You misunderstood what @JonB wrote. What he wrote is that you have to translate the C++ code you see above in its Python counterpart. That's all.

                                      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

                                      • Login

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