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. How to sort data by column in a table view if click to headerview[Solved]
QtWS25 Last Chance

How to sort data by column in a table view if click to headerview[Solved]

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.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.
  • M Offline
    M Offline
    Mittu
    wrote on last edited by
    #1

    @ Log cnn;
    QSqlQueryModel *modal=new QSqlQueryModel();
    QSqlQuery *qry=new QSqlQuery(cnn.mydb);
    qry->prepare("select *from log");
    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);
    ui->tableView->setAlternatingRowColors(true);
    ui->tableView->setSortingEnabled(true);
    ui->tableView->sortByColumn(4,Qt::AscendingOrder);//4 indicate the 4th column@

    I have a QTableView that is populated with a QSqlQueryModel. I am trying to sort the table based on which header is clicked, but nothing is happening when I press them.
    I used function sortByColumn()

    The problem is that the result is exactly the same,when i click the header the sorting is not working.
    

    What am I doing wrong?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      treenn
      wrote on last edited by
      #2

      Try to add this (for Qt 4):
      @ui->tableView->horizontalHeader()->setClickable(true);@
      Or, for Qt 5, this:
      @ui->tableView->horizontalHeader()->setSectionsClickable(true);@

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mittu
        wrote on last edited by
        #3

        Thanks...
        And also i solved my problem using QSortFilterProxyModel.

        Log cnn;
        QSqlQueryModel *modal=new QSqlQueryModel(this);
        QSqlQuery *qry=new QSqlQuery(cnn.mydb);
        qry->prepare("select *from AccessLog");
        qry->exec();
        modal->setQuery(*qry);

        QSortFilterProxyModel *m=new QSortFilterProxyModel(this);
        m->setDynamicSortFilter(true);
        m->setSourceModel(modal);
        ui->tableView->setModel(m);
        ui->tableView->setSortingEnabled(true);

        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