Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sorting problem QTableView
Forum Updated to NodeBB v4.3 + New Features

Sorting problem QTableView

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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:

    0_1553591701853_2af4114e-e9e5-4841-b1f1-6a6ce1b7c667-image.png

    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!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2
      • What model are you using?
      • Are you using a QSortFilterProxyModel?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        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

        VRoninV 1 Reply Last reply
        0
        • ivanicyI ivanicy

          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

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          QTableView != QTableWidget. In any case, what you are doing should work. Can you show us a minimal example to reproduce the problem?

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          ivanicyI 1 Reply Last reply
          1
          • VRoninV VRonin

            QTableView != QTableWidget. In any case, what you are doing should work. Can you show us a minimal example to reproduce the problem?

            ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by ivanicy
            #5

            @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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved