Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Width not correct in resizeEvent



  • 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.


  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    Hi
    You could also try

    
    void 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);
    }
    

    alt text

    But do notice its also called on maximize/show normal etc.


Log in to reply