Solved QTableWidget strange behaviour
-
Hi,
I'm having some trouble with QTableWidget ( app is crashing). In order to easily replicate the problem, I wrote a short project isolating the issue. Here is the code:mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QTableWidget> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); void on_pushButton_2_clicked(); private: Ui::MainWindow *ui; QTableWidget table; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QHeaderView> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); table.show(); table.setColumnCount(5); table.setHorizontalHeaderLabels(QStringList{"col1","col2","col3","col4","col5"}); table.horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); table.setSortingEnabled(true); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { table.clearContents(); table.setRowCount(0); for(int i=0;i<10;++i) { int row = table.rowCount(); table.insertRow(row); QTableWidgetItem* item = new QTableWidgetItem("Title"+QString::number(i)); table.setItem(row, 0,item); for(int i=1;i<=4;++i) table.setItem(row,i, new QTableWidgetItem()); } } void MainWindow::on_pushButton_2_clicked() { table.setSortingEnabled(false); for(int i = 0;i<table.rowCount();i++){ table.item(i,1)->setText("Value"+QString::number(i)); table.item(i,2)->setText("Value"+QString::number(i)); table.item(i,3)->setText("Value"+QString::number(i)); table.item(i,4)->setText("Value"+QString::number(i)); } table.setSortingEnabled(true); table.show(); }
The UI only contains two buttons. Here is what's happening: I press the first button, and the cells are created and the first column is filled with the title strings. I press the second button and the remaining 4 columns are filled with value strings. Until now everything works ok. If I close the table, press the first button, then the second, the above behavior is repeated, as expected.
Now the strange thing: If I fill the table by clicking the two buttons, then sort the table by clicking on the column header of columns 2,3,4 (middle 3 columns) , then close the table, and the press again on the two buttons, the app crashes. Sorting by the first or last column works ok. Also, the fact that the cells are already sorted doesn't make a difference. Here is where the app crashes:
If table was sorted by columns 2,3,4, the debugger's yellow arrow will indicate lines 50,51, or 52 respectively.
What's causing all this ?
-
Hi
how do you "close" the table ?
Seems to be an embedded widget ?Did you check that table.item(i,1) does return a valid item after you sorted?
-
The table is a stand alone widget(declared in mainwindow.h) which I display using the show() function. I was closing it by clicking on the top right red X. But in fact, ignore the closing step, because I just checked, and the issue also occurs without closing the table.
-
Hi
I could reproduce it.
It seems that table.item returns NULL item.You can see it if you do.
void setText(QTableWidgetItem *item, QString text ) { if (item) item->setText(text); else qDebug() << "item is null"; } void MainWindow::on_pushButton_2_clicked() { table.setSortingEnabled(false); for (int i = 0; i < table.rowCount(); i++) { setText( table.item(i, 2), "Value" + QString::number(i)); setText( table.item(i, 3), "Value" + QString::number(i)); setText( table.item(i, 4), "Value" + QString::number(i)); } table.setSortingEnabled(true); table.show(); }
I assume it happens as you then in second round insert items with sorting on (first button) It seems to go away if i do void MainWindow::on_pushButton_clicked() { table.clearContents(); table.setRowCount(0); table.setSortingEnabled(false); // oFF for (int i = 0; i < 10; ++i) { int row = table.rowCount(); table.insertRow(row); QTableWidgetItem *item = new QTableWidgetItem("Title" + QString::number(i)); table.setItem(row, 0, item); for (int i = 1; i < table.columnCount(); ++i) table.setItem(row, i, new QTableWidgetItem("X")); } table.setSortingEnabled(true); // on again }
so i think its best to have sort off while inserting items as else it will sort them while you insert which might mess up index and crash.
-
Thanks a lot @mrjj, that solves the problem indeed. Strange however that it happens only when sorting using the middle 3 columns.
-
@cpper
Yeah I also found that odd as sorting on the first should also do it.
But we since we create them empty
for (int i = 1; i <= 4; ++i)
table.setItem(row, i, new QTableWidgetItem());and then fill them later in step 2, it might be related but i did not look into that further.