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. Bounding size of QTableView to parent widget
Forum Updated to NodeBB v4.3 + New Features

Bounding size of QTableView to parent widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 532 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.
  • V Offline
    V Offline
    vivaat
    wrote 27 days ago last edited by
    #1

    I am trying to make a table view stay within its display are, no more, no less. The problem for now is that this view can be stretched beyond the bounds of its parent widget. See, for example, a trivial case:

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
    
        auto model = new QStandardItemModel;
        model->setHorizontalHeaderItem(0, new QStandardItem("Zero"));
        model->setHorizontalHeaderItem(1, new QStandardItem("One"));
        model->setHorizontalHeaderItem(2, new QStandardItem("Two"));
        model->setHorizontalHeaderItem(3, new QStandardItem("Three"));
        for (int row = 0; row < 4; ++row) {
            QList<QStandardItem *> rowData;
            rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(0));
            rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(1));
            rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(2));
            rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(3));
            model->appendRow(rowData);
        }
    
        auto window = new QWidget;
    
        auto table = new QTableView{};
        table->horizontalHeader()->setStretchLastSection(true);
        table->setModel(model);
    
        auto vbox = new QHBoxLayout(window);
        vbox->setContentsMargins(50, 50, 50, 50);
        vbox->addWidget(table);
    
        window->show();
        return a.exec();
    }
    

    I can still drag the rightmost side of the table view out of the margins of the layout. And it will stay there, depriving me of any ability to resize back.

    Would appreciate an advice on how to combat that!

    C J 2 Replies Last reply 27 days ago
    0
    • V vivaat
      27 days ago

      I am trying to make a table view stay within its display are, no more, no less. The problem for now is that this view can be stretched beyond the bounds of its parent widget. See, for example, a trivial case:

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
      
          auto model = new QStandardItemModel;
          model->setHorizontalHeaderItem(0, new QStandardItem("Zero"));
          model->setHorizontalHeaderItem(1, new QStandardItem("One"));
          model->setHorizontalHeaderItem(2, new QStandardItem("Two"));
          model->setHorizontalHeaderItem(3, new QStandardItem("Three"));
          for (int row = 0; row < 4; ++row) {
              QList<QStandardItem *> rowData;
              rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(0));
              rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(1));
              rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(2));
              rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(3));
              model->appendRow(rowData);
          }
      
          auto window = new QWidget;
      
          auto table = new QTableView{};
          table->horizontalHeader()->setStretchLastSection(true);
          table->setModel(model);
      
          auto vbox = new QHBoxLayout(window);
          vbox->setContentsMargins(50, 50, 50, 50);
          vbox->addWidget(table);
      
          window->show();
          return a.exec();
      }
      

      I can still drag the rightmost side of the table view out of the margins of the layout. And it will stay there, depriving me of any ability to resize back.

      Would appreciate an advice on how to combat that!

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote 27 days ago last edited by
      #2

      @vivaat said in Bounding size of QTableView to parent widget:

      I can still drag the rightmost side of the table view out of the margins of the layout

      No, not in your example (I had to remove the undefined MainWindow w from your code):

      b376d9d1-a62b-45fa-b3c7-b75c5a9e926f-grafik.png

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      V 1 Reply Last reply 26 days ago
      1
      • V vivaat
        27 days ago

        I am trying to make a table view stay within its display are, no more, no less. The problem for now is that this view can be stretched beyond the bounds of its parent widget. See, for example, a trivial case:

        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
        
            auto model = new QStandardItemModel;
            model->setHorizontalHeaderItem(0, new QStandardItem("Zero"));
            model->setHorizontalHeaderItem(1, new QStandardItem("One"));
            model->setHorizontalHeaderItem(2, new QStandardItem("Two"));
            model->setHorizontalHeaderItem(3, new QStandardItem("Three"));
            for (int row = 0; row < 4; ++row) {
                QList<QStandardItem *> rowData;
                rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(0));
                rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(1));
                rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(2));
                rowData << new QStandardItem(QString("row %1, column %2").arg(row).arg(3));
                model->appendRow(rowData);
            }
        
            auto window = new QWidget;
        
            auto table = new QTableView{};
            table->horizontalHeader()->setStretchLastSection(true);
            table->setModel(model);
        
            auto vbox = new QHBoxLayout(window);
            vbox->setContentsMargins(50, 50, 50, 50);
            vbox->addWidget(table);
        
            window->show();
            return a.exec();
        }
        

        I can still drag the rightmost side of the table view out of the margins of the layout. And it will stay there, depriving me of any ability to resize back.

        Would appreciate an advice on how to combat that!

        J Offline
        J Offline
        JonB
        wrote 27 days ago last edited by
        #3

        @vivaat
        I would not expect that from a glance at your code, which seems fine. You seem only to be asking for a QTableView to stay within a parent QWidget which does have a layout set, that should be working. You might state your platform and Qt version?

        V 1 Reply Last reply 26 days ago
        0
        • C Christian Ehrlicher
          27 days ago

          @vivaat said in Bounding size of QTableView to parent widget:

          I can still drag the rightmost side of the table view out of the margins of the layout

          No, not in your example (I had to remove the undefined MainWindow w from your code):

          b376d9d1-a62b-45fa-b3c7-b75c5a9e926f-grafik.png

          V Offline
          V Offline
          vivaat
          wrote 26 days ago last edited by
          #4

          @Christian-Ehrlicher
          Sorry for MainWindow, forgot about it) The problem reproduces if you switch it to QMainWindow.

          From your screenshot, it seems that I haven't stated my problem well enough. It is not that when you minimize the window, the table is not minimized. The problem is that when you open the window in a way that the table is fully visible and the last column is stretched, the last column's right side can be stretched outside the margins of the bounding widget.

          Please look at these screenshots:
          b8ff3db4-bcc3-4504-b904-553afad2f901-Untitled.png

          This may look as a odd corner case, but in a real scenario it is more vivid. Please see the attached gif for the real world case.

          Recording 2025-06-03 100916.gif

          1 Reply Last reply
          0
          • J JonB
            27 days ago

            @vivaat
            I would not expect that from a glance at your code, which seems fine. You seem only to be asking for a QTableView to stay within a parent QWidget which does have a layout set, that should be working. You might state your platform and Qt version?

            V Offline
            V Offline
            vivaat
            wrote 26 days ago last edited by
            #5

            @JonB

            My platform is Windows 11, using QtC 16.0.0 based on Qt 6.8.2 to compile and run this example.

            1 Reply Last reply
            0
            • C Online
              C Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote 26 days ago last edited by
              #6

              I don't see a problem here - even the windows explorer allows resizing a section behind the view.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              V 1 Reply Last reply 24 days ago
              2
              • C Christian Ehrlicher
                26 days ago

                I don't see a problem here - even the windows explorer allows resizing a section behind the view.

                V Offline
                V Offline
                vivaat
                wrote 24 days ago last edited by
                #7

                @Christian-Ehrlicher
                But is it possible to limit such a behavior?

                C 1 Reply Last reply 24 days ago
                0
                • V vivaat
                  24 days ago

                  @Christian-Ehrlicher
                  But is it possible to limit such a behavior?

                  C Online
                  C Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote 24 days ago last edited by
                  #8

                  @vivaat said in Bounding size of QTableView to parent widget:

                  But is it possible to limit such a behavior?

                  Maybe by overriding the mouseMove event.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2

                  1/8

                  2 Jun 2025, 12:25

                  • Login

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