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 Set Display Order In QSqlRelation Within QSqlRelationalTableModel
Qt 6.11 is out! See what's new in the release blog

How To Set Display Order In QSqlRelation Within QSqlRelationalTableModel

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

    I am using QSqlRelation to provide the users with a dropdown box showing the valid entries within a browse window. How do you put the entries in some meaningful order? All I can get it to do is to display the information in the physical order they were entered into the table.

    See code below. The data in mf_work_center, tt_operator, and mf_cells all display their data in the physical order the data was entered into the table. I want it to be ordered by the work center in the first, the operation in the second, and the cell number in the third.

    ipmodel = new QSqlRelationalTableModel(this);
    ipmodel->setTable("wo_routing");
    ipmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    ipmodel->setRelation(6, QSqlRelation("mf_work_center", "work_center", "work_center"));
    ipmodel->setRelation(5, QSqlRelation("tt_operator", "operator", "operator"));
    ipmodel->setRelation(7, QSqlRelation("mf_cells", "cells", "cells"));
    ipmodel->setRelation(8, QSqlRelation("mf_machine", "machine", "machine"));
    ipmodel->setSort(2,Qt::AscendingOrder);
    ipmodel->setHeaderData(Work_Center, Qt::Horizontal, tr("Work Center"));
    ipmodel->setHeaderData(Cell, Qt::Horizontal, tr("  Cell  "));
    ipmodel->setHeaderData(Operation, Qt::Horizontal, tr("Operation"));
    ipmodel->setHeaderData(Machine, Qt::Horizontal, tr("Machine      "));
    ipmodel->setHeaderData(Throughput, Qt::Horizontal, tr("Throughput - PT/HR"));
    ipmodel->setHeaderData(Setup_Time, Qt::Horizontal, tr("Setup Hrs"));
    ipmodel->setHeaderData(PM_Ratio, Qt::Horizontal, tr("PM Ratio/1"));
    ipmodel->setFilter(updateStr);
    ipmodel->select();
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Put a proxy-model between your model and the view. You need to subclass QSortFilterProxyModel and reimplement lessThan

      "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
      • Scott KriseS Offline
        Scott KriseS Offline
        Scott Krise
        wrote on last edited by
        #3

        Im trying to do as you suggested...but my syntax must be incorrect. See below...

        You understand I want the information within the mf_work_center, tt_operator, and mf_cells tables to appear in a sorted order once they open the pull-down menu, right? Not sure that was clear. Thanks

        ipmodel = new QSqlRelationalTableModel(this);
        ipmodel->setTable("wo_routing");
        ipmodel->setEditStrategy(QSqlTableModel::OnManualSubmit);
        ipmodel->setRelation(6, QSqlRelation("mf_work_center", "work_center", "work_center"));
        ipmodel->setRelation(5, QSqlRelation("tt_operator", "operator", "operator"));
        ipmodel->setRelation(7, QSqlRelation("mf_cells", "cells", "cells"));
        ipmodel->setRelation(8, QSqlRelation("mf_machine", "machine", "machine"));
        ipmodel->setSort(2,Qt::AscendingOrder);
        ipmodel->setHeaderData(Work_Center, Qt::Horizontal, tr("Work Center"));
        ipmodel->setHeaderData(Cell, Qt::Horizontal, tr("  Cell  "));
        ipmodel->setHeaderData(Operation, Qt::Horizontal, tr("Operation"));
        ipmodel->setHeaderData(Machine, Qt::Horizontal, tr("Machine      "));
        ipmodel->setHeaderData(Throughput, Qt::Horizontal, tr("Throughput - PT/HR"));
        ipmodel->setHeaderData(Setup_Time, Qt::Horizontal, tr("Setup Hrs"));
        ipmodel->setHeaderData(PM_Ratio, Qt::Horizontal, tr("PM Ratio/1"));
        ipmodel->setFilter(updateStr);
        ipmodel->select();
        

        //--- this was added to try to sort the mf_work_center data while displaying pulldown

        ipModelProxy = new QSortFilterProxyModel(this);
        ipModelProxy->setSourceModel(ipmodel);
        ipModelProxy->sort(6, Qt::AscendingOrder);
        
        ui->workorderLineEdit->setText(workorder);
        
        ui->iptableView->close();
        ui->iptableView->setModel(ipmodel);
        ui->iptableView->setItemDelegate(new QSqlRelationalDelegate(ui->iptableView));
        ui->iptableView->resizeColumnsToContents();
        ui->iptableView->setAlternatingRowColors(true);
        ui->iptableView->setSortingEnabled(true);
        
        ui->iptableView->setColumnHidden(Company,true);
        ui->iptableView->setColumnHidden(Release,true);
        ui->iptableView->setColumnHidden(WorkOrder,true);
        ui->iptableView->setColumnHidden(RowId,true);
        ui->iptableView->setColumnHidden(PM_Part_Number,true);
        ui->iptableView->setColumnHidden(PM_UPC,true);
        ui->iptableView->setColumnHidden(LineNumber,true);
        ui->iptableView->setColumnHidden(Percent,true);
        ui->iptableView->setColumnHidden(Part_Number,true);
        ui->iptableView->setColumnHidden(Sched_Date,true);
        ui->iptableView->setColumnHidden(Priority,true);
        ui->iptableView->setColumnWidth(7,50);
        ui->iptableView->show();
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @Scott-Krise said in How To Set Display Order In QSqlRelation Within QSqlRelationalTableModel:

          You understand I want the information within the mf_work_center, tt_operator, and mf_cells tables to appear in a sorted order once they open the pull-down menu, right?

          I did not, no, I thought you wanted to sort the columns.

          Try the below to replace your QSqlRelationalDelegates:

          class SortedRelationDelegate : public QSqlRelationalDelegate{
              Q_OBJECT
              Q_DISABLE_COPY(SortedRelationDelegate)
          public:
              SortedRelationDelegate(QObject* parent = Q_NULLPTR)
                  :QSqlRelationalDelegate(parent)
              {}
              QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const Q_DECL_OVERRIDE {
                  QWidget* const base = QSqlRelationalDelegate::createEditor(parent,option,index);
                  QComboBox* const combo = qobject_cast<QComboBox*>(base);
                  Q_ASSERT(combo);
                  QSortFilterProxyModel* sortProxy = new QSortFilterProxyModel(parent);
                  sortProxy->setSourceModel(combo->model());
                  sortProxy->sort(0);
                  combo->setModel(sortProxy);
                  return combo;
              }
          };
          

          here I used a straight QSortFilterProxyModel to sort the results but you are free to use a subclass that does the sorting you prefer

          "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

          • Login

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