Results 1 to 4 of 4

Thread: Newbie TableView question

  1. #1
    Join Date
    Jun 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Question Newbie TableView question

    I'm new to Qt but have been a C++ coder since Borland... my question is how would one assign certain fields in a database ( I'm using SQlite ) to particular columns... ie changing the display order. This is a desktop application.

    Let me know if I'm using the wrong class for what I need to do.... tnx.

    code snippit follows:


    // 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"));// set Relation

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

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie TableView question

    This looks reasonable, based on what I have to guess about your DB structure. Without some context around where this code lives, it's hard to say more.

    Is it doing what you want?

    Please use CODE tags when posting source code for better readability. See my signature block for how to do that.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2020
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie TableView question

    Thanks for the reminder on the code tags. My question is how would I change the order of the column display...
    IE let's say the database order is ID Name City and I want to display Name City ID So what wuld be the way to assign certain data fields to certain columns.

    Thanks.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,236
    Thanks
    303
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Newbie TableView question

    My question is how would I change the order of the column display...
    Probably the simplest way would be to use a proxy model, derived from QAbstractProxyModel or one of its derived classes. The methods you would need to override are mapFromSource() / mapToSource() where you swap the columns appropriately. If you want to exclude rows / columns that are in the SQL model, then you should use QSortFilterProxyModel as the base class for your derived model, and implement the filterAcceptsRow() / filterAcceptsColumn() methods.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Newbie TableView question
    By Colorado_Al in forum The Model-View Framework
    Replies: 0
    Last Post: 9th June 2020, 20:42
  2. Newbie question regarding Py Qt4 Designer
    By dennisvz in forum Newbie
    Replies: 3
    Last Post: 30th January 2017, 09:27
  3. Newbie question about QToolBar
    By AlbertoN in forum Newbie
    Replies: 1
    Last Post: 28th May 2012, 06:38
  4. REAL Newbie question -- where to go from here?
    By scott_hollen in forum Newbie
    Replies: 4
    Last Post: 21st January 2011, 08:35
  5. Tipical newbie question
    By Dark_Tower in forum Newbie
    Replies: 2
    Last Post: 24th March 2006, 06:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.