Width not correct in resizeEvent
Solved
General and Desktop
-
I'm trying to get the width of my table during initialization so I can size my columns at a 1:2:2 ratio.
void MainWindow::resizeEvent(QResizeEvent* event) { QMainWindow::resizeEvent(event); QTableWidget* table = ui->GlobalsView; int width = table->width(); table->setColumnWidth(0, width / 5); table->setColumnWidth(1, width / 5 * 2); table->setColumnWidth(2, width / 5 * 2); }
But table->width() is only giving a width of 100 when the width is actually like 350.
If I trigger the resize event again after initialization by dragging the screen around it gives me the correct width, but it shouldn't require that. -
Hi,
I'd add an event filter for the QTableWidget's resize event and trigger that code there so you ensure that you are acting directly on the concerned widget.
Hope it helps
-
Hi
You could also tryvoid MainWindow::showEvent(QShowEvent *) { QTableWidget* table = ui->tableWidget; int width = table->width(); table->setColumnWidth(0, width / 5); table->setColumnWidth(1, width / 5 * 2); table->setColumnWidth(2, width / 5 * 2); }
But do notice its also called on maximize/show normal etc.