Sort by column header in tablemodel



  • I've checked the forums and documentation but still can't seem to get it to work. Supposed to be essentially "automatic" and "built-in" using one line of code but the sort function just doesn't happen. The arrow indicators do show up but that's all that happens.

    Thank you for your help.

    Here's the code:

    SQLite_Login.Pro

    @
    QT += core gui sql

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = SQLIte_Login
    TEMPLATE = app

    SOURCES += main.cpp
    login.cpp
    customerinfo.cpp

    HEADERS += login.h
    customerinfo.h

    FORMS += login.ui
    customerinfo.ui

    RC_FILE = nhx.rc@

    main.cpp

    @#include "login.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Login w;
    w.show();

    return a.exec&#40;&#41;;
    

    }@

    customerinfo.cpp

    @
    #include "customerinfo.h"
    #include "ui_customerinfo.h"
    #include<QMessageBox>

    #include <QDesktopServices>
    #include <QUrl>

    #include <QFileDialog>

    #include <QString>

    void CustomerInfo::on_pushButton_Load_Tbl_clicked()
    {
    Login conn;
    QSqlQueryModel * modal=new QSqlQueryModel();

    conn.connOpen();
    QSqlQuery* qry=new QSqlQuery(conn.mydb);

    qry->prepare("SELECT LotNo, UserName, LastName, Role from Test_LOGIN order by LotNo ");

    qry->exec();
    modal->setQuery(*qry);
    ui->tableView->setModel(modal);

     ui->tableView->setSortingEnabled(true);
     ui->tableView->sortByColumn(true);
    

    conn.connClose();
    qDebug()<<(modal->rowCount());

    }@



  • nHx4U,

    I can not understand what is your problem is. If you can clarify more, I by myself or someone can try to help you.



  • Hi.

    Thanks for the reply.

    I can load data from a SQLite table in a TableView on a gui form. The problem is that you are supposed to be able to re-sort the table by clicking on the column headers. To toggle from ascending to descending.

    The sort direction triangles (up/down arrows) show up in the header but clicking on it does not re-sort the table.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.