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. Width not correct in resizeEvent
QtWS25 Last Chance

Width not correct in resizeEvent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 717 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.
  • L Offline
    L Offline
    Luke Puchard
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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.

        1 Reply Last reply
        1

        • Login

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