How To Set Display Order In QSqlRelation Within QSqlRelationalTableModel



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


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



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


  • @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


Log in to reply
 

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