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 476 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 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!

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • V vivaat

      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!

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote 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
      1
      • V vivaat

        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!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote 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
        0
        • Christian EhrlicherC Christian Ehrlicher

          @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 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
          • JonBJ JonB

            @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 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
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote 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
              2
              • Christian EhrlicherC Christian Ehrlicher

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

                V Offline
                V Offline
                vivaat
                wrote last edited by
                #7

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

                Christian EhrlicherC 1 Reply Last reply
                0
                • V vivaat

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

                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote 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

                  • Login

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