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. Sort by column header in tablemodel
QtWS25 Last Chance

Sort by column header in tablemodel

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

    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());

    }@

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hareen Laks
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nHx4U
        wrote on last edited by
        #3

        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.

        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