Important: Please read the Qt Code of Conduct -

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:
    // contains the data I want to insert (it is correct)
    QTableWidgetItem *itemColumn0 = new QTableWidgetItem(;
    QTableWidgetItem *itemColumn1 = new QTableWidgetItem(;
    QTableWidgetItem *itemColumn2 = new QTableWidgetItem(;
    QTableWidgetItem *itemColumn3 = new QTableWidgetItem(;
    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)

    One detail more, if I use:


    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.

Log in to reply