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 by clicking column heading in QSqlQueryModel
Forum Updated to NodeBB v4.3 + New Features

Sorting by clicking column heading in QSqlQueryModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 587 Views 1 Watching
  • 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.
  • Scott KriseS Offline
    Scott KriseS Offline
    Scott Krise
    wrote on last edited by
    #1

    Hello Everyone,

    So I recently designed a screen using QSqlTableModel and I was able to use ui->tableView->setSortingEnabled(true) to allow the user to be able to click any column heading and have the data sorted by that field.

    I am now working on a new screen that is a little more complex, and I needed to switch to QSqlQueryModel because of some additional requirements. When I did that, I lost the ability to click the heading to sort.

    What must I do to be able to sort by column headings. Here is my code using the QSqlQueryModel.

    model = new QSqlQueryModel();
    updateStr = QString("select t.sched_date, t.toshipdate, t.workorder, t.name, t.upc, t.seq, t.operation, t.description, t.cell, t.work_center, t.machine, t.dept_name from tt_schedule t WHERE t.user = '%1' ORDER BY t.dept_name, t.sched_date, t.workorder, t.seq, t.toshipdate").arg(userName);

    model->setQuery(updateStr);
    model->setHeaderData(0, Qt::Horizontal, QObject::tr("Schedule Date"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("ToShip Date"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Work Order"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("Customer"));
    model->setHeaderData(4, Qt::Horizontal, QObject::tr("UPC"));
    model->setHeaderData(5, Qt::Horizontal, QObject::tr("Seqn"));
    model->setHeaderData(6, Qt::Horizontal, QObject::tr("Operation"));
    model->setHeaderData(7, Qt::Horizontal, QObject::tr("Description"));
    model->setHeaderData(8, Qt::Horizontal, QObject::tr("Cell"));
    model->setHeaderData(9, Qt::Horizontal, QObject::tr("Work Center"));
    model->setHeaderData(10, Qt::Horizontal, QObject::tr("Machine"));
    model->setHeaderData(11, Qt::Horizontal, QObject::tr("Department                     "));
    
    ui->tableView->close();
    ui->tableView->setModel(model);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->resizeColumnsToContents();
    
    ui->tableView->verticalHeader()->hide();
    ui->tableView->setAlternatingRowColors(true);
    ui->tableView->setSortingEnabled(true);
    ui->tableView->show();
    
    mapper = new QDataWidgetMapper(this);
    mapper->setModel(model);
    mapper->addMapping(ui->upcLineEdit,4);
    mapper->addMapping(ui->workOrderLineEdit,2);
    
    connect(ui->tableView->selectionModel(),  SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),mapper, SLOT(setCurrentModelIndex(QModelIndex)));
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAIK, you need to subclass QSqlQueryModel and implement the sort method to do what you want.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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