Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TableView, SQLite, QSqlRelationalTableModel



  • I'm new to QT but started with Borland C.

    I have a desktop app that makes use of TableViews and a database with lots of relations.
    My question is how to assign a certain field to a certain column.. ie changing the display order.

    Here's a code snippit:

    // QSqlRelationalTableModel ////////////////////////////////////////////////////////////////////////////////////////////////
    QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
    model->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    model->setTable("tblSales");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);

    model->setRelation(4,   QSqlRelation("tblJewelryPiece", "JewelryPieceID", "Category"));// set Relation
    model->setRelation(5,   QSqlRelation("tblJewelryPiece", "JewelryPieceID", "PieceName"));
    
    model->setHeaderData(0, Qt::Horizontal, tr("Invoice #"));                               // set Header Data
    model->setHeaderData(2, Qt::Horizontal, tr("Date"));
    model->setHeaderData(3, Qt::Horizontal, tr("Qty"));
    model->setHeaderData(4, Qt::Horizontal, tr("Category"));
    model->setHeaderData(6, Qt::Horizontal, tr("Jewelry Piece"));
    model->setHeaderData(7, Qt::Horizontal, tr("Price"));
    
    model->setFilter("CustomerID=" + val);                            // set filter to current customer
    
    model->select();                                                                        // select
    ui->tv_Sales->setModel(model);
     ui->tv_Sales->resizeColumnsToContents();
     ui->tv_Sales->show();

  • Lifetime Qt Champion

    I would go with QHeaderView::moveSection()



  • Thanks, will give that a shot... ( still learning the classes ).

    Thanks!


Log in to reply