Bounding size of QTableView to parent widget
-
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!
-
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!
@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): -
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!
-
@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):@Christian-Ehrlicher
Sorry forMainWindow
, forgot about it) The problem reproduces if you switch it toQMainWindow
.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:
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.
-
@vivaat
I would not expect that from a glance at your code, which seems fine. You seem only to be asking for aQTableView
to stay within a parentQWidget
which does have a layout set, that should be working. You might state your platform and Qt version? -
I don't see a problem here - even the windows explorer allows resizing a section behind the view.
-
I don't see a problem here - even the windows explorer allows resizing a section behind the view.
@Christian-Ehrlicher
But is it possible to limit such a behavior? -
@Christian-Ehrlicher
But is it possible to limit such a behavior?@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.