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