Unsolved Sorting problem QTableView
-
Hello!!
I am using a QTableView to show some entries of data. I have enabled the sorting but I have a problem. When I want to refresh the table with new data, if I have clicked on a column for sort by it, the table doesn't show the right columns. Look at the image:
I have sorted by "Date Time" column, and when I refresh, it appears like that.
How can I reset the sorting, or something like this, to solve this problem?
Thank you very much!
-
- What model are you using?
- Are you using a
QSortFilterProxyModel
?
-
I am using the default QTableView widget and its methods. I use tableWidget->insertRow() and this kind of methods. I don't know how to use a QSortFilterProxyModel
-
QTableView
!=QTableWidget
. In any case, what you are doing should work. Can you show us a minimal example to reproduce the problem? -
@VRonin Yes, I had a mistake. I am using a QTABLEWIDGET.
The problem is when I want to refresh the table after doing a column sort. So, I can't extract a compilable example, but I can show the steps I am following to do it:
// I have a QStringList vector to save all the rows I want to insert in the table. This is because I apply filters on the information. std::vector<QStringList> m_vectorTable; // In the refresh function: m_vectorTable.clear(); ui->tableEvents->setRowCount(0); ... ui->tableEvents->insertRow(0); // slLine.at(x) contains the data I want to insert (it is correct) QTableWidgetItem *itemColumn0 = new QTableWidgetItem(slLine.at(0)); QTableWidgetItem *itemColumn1 = new QTableWidgetItem(slLine.at(1)); QTableWidgetItem *itemColumn2 = new QTableWidgetItem(slLine.at(3)); QTableWidgetItem *itemColumn3 = new QTableWidgetItem(slLine.at(4)); itemColumn0->setBackgroundColor(QColor(255,0,0,100)); itemColumn1->setBackgroundColor(QColor(255,0,0,100)); itemColumn2->setBackgroundColor(QColor(255,0,0,100)); itemColumn3->setBackgroundColor(QColor(255,0,0,100)); ui->tableEvents->setItem(0, 0, itemColumn0); ui->tableEvents->setItem(0, 1, itemColumn1); ui->tableEvents->setItem(0, 2, itemColumn2); ui->tableEvents->setItem(0, 3, itemColumn3); // slLineToVector contains the row (its columns are correct) m_vectorTable.push_back(slLineToVector); ui->tableEvents->repaint();
One detail more, if I use:
ui->tableEvents->setSortingEnabled(false); ui->tableEvents->setSortingEnabled(true);
at the beginning and the end of the refresh function, it solve the problem, but keeps the sort configuration. I want to restart the sort configuration to the default one.