Results 1 to 9 of 9

Thread: Adding columns to qtableview

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Adding columns to qtableview

    I would like to know if there is a way to add columns in qtableview that are not in an SQL table.

    My problem is this:

    I have a query that will return a number of product names. I want that to be the first column in a qtableview.
    Then I want to have 3 more columns that I can edit such as setting a checkmark of which products to use and how many.

    I will not be saving back to the database the table.

    Any ideas will be helpful.

    Thanks,

    Pericles

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding columns to qtableview

    do you mean QTableView or QTableWidget?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding columns to qtableview

    Hmm after seeing your comment I looked into QTableWidget. It seems that QTableWidget allows you to enter data one by one so it might be able to add extra columns. I was using QTableView until now. QTableView only shows model data so that might have been the problem.

    I will try QTableWidget and see how it goes.

    Thanks

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding columns to qtableview

    . QTableView only shows model data so that might have been the problem.
    QTableWidgte also only shows model data.
    But it implements common table use cases and offer API for it.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Sep 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding columns to qtableview

    Qt Code:
    To copy to clipboard, switch view to plain text mode 
    Also you can use QStandardItemModel::insertRows() and / or QStandardItemModel::insertColumns() to add rows and / or columns respectively.
    Then you can set data with roles like
    Qt Code:
    1. model->setData(index, Qt::Checked, Qt::CheckStateRole);
    2. model->setData(index, "any string", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    and so on...
    You can work with row and col like this:
    Qt Code:
    1. model->setData(model->index(row, col), "any string", Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 
    These must be enough to populate your model with the mysql data, then model must be passed to view to see what you did. (QTableView::setModel())

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding columns to qtableview

    Quote Originally Posted by pcheng View Post
    I would like to know if there is a way to add columns in qtableview that are not in an SQL table.

    My problem is this:

    I have a query that will return a number of product names. I want that to be the first column in a qtableview.
    Then I want to have 3 more columns that I can edit such as setting a checkmark of which products to use and how many.

    I will not be saving back to the database the table.

    Any ideas will be helpful.

    Thanks,

    Pericles
    You need to subclass the model you are using (presumably QSqlQueryModel or QSqlTableModel) and reimplement columnCount(), data(), index() and possibly setData() and flags() depending whether you want your additional columns to be editable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding columns to qtableview

    I ended up using QTableWidget in my code. I made one of the columns a spinbox following an example I found online. The widget seems to work fine and the spinbox works. But when I try to read the values that are in the spinbox I get nothing. Is there something that I am doing wrong?

    Qt Code:
    1. // Populate the table widget with the products
    2. int row = 0;
    3. while (query.next()) {
    4. QSpinBox *sb = new QSpinBox();
    5. ui->tableWidget->setCellWidget(row,3, sb);
    6. connect(sb, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
    7. signalMapper->setMapping(sb, row);
    8. QTableWidgetItem *item = new QTableWidgetItem(tr("%1").arg(query.value(3).toString()));
    9. QTableWidgetItem *item2 = new QTableWidgetItem(tr("%1").arg(itemSizes[query.value(6).toInt()]));
    10. QTableWidgetItem *item3 = new QTableWidgetItem(tr("%1").arg(query.value(8).toInt()));
    11. QTableWidgetItem *item4 = new QTableWidgetItem(tr("%1").arg(0));
    12. ui->tableWidget->setItem(row, 0, item);
    13. ui->tableWidget->setItem(row, 1, item2);
    14. ui->tableWidget->setItem(row, 2, item3);
    15. ui->tableWidget->setItem(row, 3, item4);
    16. row++;
    17. }
    18.  
    19. ui->tableWidget->resizeColumnsToContents();
    20. ui->tableWidget->setAlternatingRowColors(true);
    21. ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    22.  
    23. connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(recalculate(int)));
    24.  
    25. calculateTotals();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void PackageDialog::calculateTotals()
    2. {
    3. int remains = 0;
    4. int packaged = 0;
    5.  
    6. // Find the total remaining stock which is not yet packaged
    7. for (int i=0; i < ui->tableWidget->rowCount(); i++)
    8. {
    9. QTableWidgetItem *itab = ui->tableWidget->item(i,2);
    10. int itabtext = itab->text().toInt();
    11. remains += itabtext;
    12. qDebug() << "Remaining Value was" << itabtext << "Remaining is now" << remains;
    13. }
    14.  
    15. ui->remainingLineEdit->setText(QString::number(remains));
    16.  
    17. // Find the total packaged stock
    18. for (int i=0; i < ui->tableWidget->rowCount(); i++)
    19. {
    20. QTableWidgetItem *itab = ui->tableWidget->item(i,3);
    21. int itabtext = itab->text().toInt();
    22. packaged += itabtext;
    23. qDebug() << "Packaged Value was" << itabtext << "Packaged is now" << packaged;
    24. }
    25.  
    26. ui->packagedLineEdit->setText(QString::number(packaged));
    27. }
    To copy to clipboard, switch view to plain text mode 

    The first for loop in the calculateTotals function works fine whereas the second one with the spinbox always returns 0.

    Thanks,

    Pericles

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding columns to qtableview

    Do you mean this part?:
    Qt Code:
    1. // Find the total packaged stock
    2. for (int i=0; i < ui->tableWidget->rowCount(); i++)
    3. {
    4. QTableWidgetItem *itab = ui->tableWidget->item(i,3);
    5. int itabtext = itab->text().toInt();
    6. packaged += itabtext;
    7. qDebug() << "Packaged Value was" << itabtext << "Packaged is now" << packaged;
    8. }
    To copy to clipboard, switch view to plain text mode 
    You forgot(?) to get the spinbox and read the value from it.
    You are asking the item for any text it has, and has none.
    Ask the table for the spin box in this cell, and ask the spin box for its value.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding columns to qtableview

    I get what you say high Flyer. But after searching Google for a while I cannot seem to find how to get the spinbox from the cell.

    A couple of other posts here in Qt Centre have the same problems. One solution was to subclass the Spinbox and make it into a delegate while another talked about signals and slots.

    Is there an easy way to get the spinbox from the cell and get the value from that spinbox?

    Thanks,

    Pericles

    EDIT* I found another post http://www.qtcentre.org/threads/6366...Widget-signals which was very helpful. I just needed to make the cell contents numbers and the spinbox would be available with no other changes. I feel stupid but that's why I am in the Newbie section.
    Last edited by pcheng; 11th July 2012 at 09:04.

Similar Threads

  1. Distribute columns in QTableView
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 17th August 2011, 14:33
  2. Sorting columns of the QTableView
    By Qiieha in forum Qt Programming
    Replies: 8
    Last Post: 9th May 2011, 19:24
  3. Validator for QTableView columns
    By sasi in forum Qt Programming
    Replies: 0
    Last Post: 6th October 2009, 08:10
  4. Different delegates to different Columns of QTableView.
    By kaushal_gaurav in forum Qt Programming
    Replies: 4
    Last Post: 6th August 2008, 09:17
  5. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 15:29

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
  •  
Qt is a trademark of The Qt Company.