Results 1 to 2 of 2

Thread: QTableView/QTableWidget and resizeRowsToContents resizes wrongly

  1. #1
    Join Date
    Aug 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QTableView/QTableWidget and resizeRowsToContents resizes wrongly

    I have a small issue. I'm trying to resize rows to its content. So in order to do that, I use resizeRowsToContents() method. But something funny is happening. I would like to call this function from a constructor because data won't change anymore so I want to resize them only once. But when I do that, rows are indeed resized but a bit larger than they suppose to be. And when I call this function once again (button click), rows will be resize properly.
    So my question is why resizeRowsToContents doesn't work properly when it's called from a constructor?


    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5.  
    6. ui->setupUi(this);
    7. ui->tableWidget->verticalHeader()->hide();
    8. ui->tableWidget->insertRow(0);
    9. item = new QTableWidgetItem("aaa bbb ccc ddd eee fff ggg hhh iii jjj kkk lll mmm nnn ooo ppp");
    10. ui->tableWidget->setItem(0, 0, item);
    11. ui->tableWidget->resizeRowsToContents(); // resizes but the row is bigger than a content
    12. }
    13.  
    14. void MainWindow::on_pushButton_clicked() {
    15. ui->tableWidget->resizeRowsToContents(); // that will resize properly
    16. }
    To copy to clipboard, switch view to plain text mode 

    Without resize
    table1.png

    Resize called from a constructor
    table2.png

    Resize called from clicked method
    table3.png

  2. #2
    Join Date
    Aug 2015
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default [Solved] Re: QTableView/QTableWidget and resizeRowsToContents resizes wrongly

    I've found it. The code should look like this:

    Qt Code:
    1. ...
    2. ui->tableWidget->setItem(0, 0, item);
    3. show(); // show the widget so the last column can be resized and the next function will have a proper column size
    4. ui->tableWidget->resizeRowsToContents();
    5. ...
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 7
    Last Post: 18th December 2014, 16:04
  2. TapAndHold gesture wrongly generated
    By maitai in forum Qt Programming
    Replies: 1
    Last Post: 26th June 2014, 20:52
  3. [QTableView] change data position if table resizes
    By Schluchti in forum Qt Programming
    Replies: 1
    Last Post: 3rd March 2014, 15:51
  4. problem with tableWidget resizeRowsToContents
    By eugenesm in forum Qt Programming
    Replies: 0
    Last Post: 1st February 2010, 14:39
  5. QTableView - resizeRowsToContents() poor performance
    By antarctic in forum Qt Programming
    Replies: 2
    Last Post: 11th December 2009, 14:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.