Results 1 to 8 of 8

Thread: QTableView in ui with model/delegate

  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView in ui with model/delegate

    Using the Spin Box Delegate Example, I was able to create a more complex delegate system that included QDateTime, QLineEdit, and QComboBox for the columns. My class works perfectly when I use the example main.cpp from the spinbox delegate example.

    I made a window in designer with a QTableView in it, and attempted to apply the the model and delegate to it as follows:

    Qt Code:
    1. //setup interface of main window:
    2. DialogAddInventoryMass::DialogAddInventoryMass(QWidget *parent, QSqlDatabase db) : QDialog(parent) {
    3.  
    4. setupUi(this);
    5.  
    6. QStandardItemModel model(10, 9);
    7. inventoryDelegate delegate;
    8.  
    9. tableView->setModel(&model);
    10. tableView->setItemDelegate(&delegate);
    11.  
    12.  
    13.  
    14. //connect form item signals to slots that arent in the UI file here:
    15. connect(buttonBox, SIGNAL(accepted()), this, SLOT(returnVars()));
    16. connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    17.  
    18. database = db;
    19. }
    To copy to clipboard, switch view to plain text mode 

    What I expected to see (as I did using the spin box example main.cpp) would be my QTableView with 10 rows, and 9 columns, and the delegate working properly.

    What actually happens, is a blank QTableView, no rows/columns in it, and there are no errors (either in compilation or runtime). I also have cell borders enabled in the .ui file and console enabled.

    What might the reason be that the QStandardItemModel is not applied to the QTableView in my UI?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    Your model is on stack.
    Try using heap
    QStandardItemModel *model = new QStandardItemModel;

  3. The following user says thank you to aamer4yu for this useful post:

    tpf80 (5th November 2008)

  4. #3
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    ah yes your right, thanks for the tip!

  5. #4
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    ok another question, I have a combobox delegate, which I want to have the item name in the QComboBox with an ID in it. Heres my current code:

    Qt Code:
    1. void inventoryDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    2. //if its the combobox column:
    3. if (index.column() == 3) {
    4. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    5.  
    6. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    7. comboBox->addItem("", "0");
    8.  
    9. for (int i = 0; i < category->itemList1.size(); ++i) {
    10. comboBox->addItem(category->itemList1.at(i), category->itemList2.at(i));
    11. }
    12.  
    13. comboBox->setCurrentIndex(comboBox->findText(value));
    14.  
    15. //if its not a combobox column
    16. } else {
    17. //do stuff for non-combobox delegates
    18. }
    19. }
    20.  
    21. void inventoryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    22. if (index.column() == 3) {
    23. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    24. model->setData(index, comboBox->currentText(), Qt::EditRole);
    25.  
    26. //if its not a combobox column
    27. } else {
    28. //do stuff for non-combobox delegates
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 
    Notice that in setEditorData, I use:
    Qt Code:
    1. comboBox->setCurrentIndex(comboBox->findText(value));
    To copy to clipboard, switch view to plain text mode 
    and in setModelData I use:
    Qt Code:
    1. model->setData(index, comboBox->currentText(), Qt::EditRole);
    To copy to clipboard, switch view to plain text mode 

    what I would like to be able to do, is when I setData to the model be able to store the QComboBox text/data pair that was selected to the model, so that in setEditorData i could use:
    Qt Code:
    1. comboBox->setCurrentIndex(comboBox->findData(data));
    To copy to clipboard, switch view to plain text mode 
    to not only make sure that the correct listing in the combobox is selected when it populates, but have the user be able to see the selected "Text" in the table cell, but the program uses the "data" associated with it for its internal uses.

    How can I store data in a QStandardItemModel cell in a similar fashion as the items are in a QComboBox item?
    Last edited by tpf80; 5th November 2008 at 20:31.

  6. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    I didnt fully understand what you require.
    But if you are talking about displaying some data, and using some other data, you can use roles.

    For text to appear, Qt has Qt::DisplayRole.You can define your own roles and set data for it.
    something like -
    QString value = index.data(MYROLE).toString();

    Hope you get the idea
    Last edited by aamer4yu; 6th November 2008 at 07:28.

  7. #6
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    I have modified my code to the following, using the custom role:
    Qt Code:
    1. enum {
    2. comboDataRole = Qt::UserRole
    3. };
    4.  
    5. void inventoryDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    6. //grab data from the model
    7. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    8. QString data = index.model()->data(index, comboDataRole).toString();
    9. //make a combobox, add a blank line as the first item in case there has not been an item selected
    10. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    11. comboBox->addItem("", "0");
    12. //fill the combobox with our stored list items
    13. for (int i = 0; i < category.itemList1.size(); ++i) {
    14. comboBox->addItem(category.itemList1.at(i), category.itemList2.at(i));
    15. }
    16. //move the selected item of the combobox to the contents stored already in the cell
    17. comboBox->setCurrentIndex(comboBox->findData(data));
    18.  
    19. }
    20.  
    21. void inventoryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
    22.  
    23. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    24. //store the text in a user viewable role, and the itemdata somewhere the user can't see
    25. model->setData(index, comboBox->currentText(), Qt::EditRole);
    26. model->setData(index, comboBox->itemData(comboBox->currentIndex()).toString(), comboDataRole);
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 
    Somehow the itemdata from the QComboBox is either not storing in the model in setModelData, or not being retrieved from the model in setEditorData. What else might I be missing to make this work?

  8. #7
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    actually from further testing, it *can* store the data as I want, but only if I select the same item in the combobox 2 times in a row, otherwise the data will be retrieved as whatever was previously in the model, rather than what was last selected. Still trying to figure out why this is..

  9. #8
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView in ui with model/delegate

    Now its working. When i changed this:
    Qt Code:
    1. model->setData(index, comboBox->currentText(), Qt::EditRole);
    2. model->setData(index, comboBox->itemData(comboBox->currentIndex()).toString(), Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 
    to this:
    Qt Code:
    1. QString combotext = comboBox->currentText();
    2. QString combodata = comboBox->itemData(comboBox->currentIndex()).toString();
    3.  
    4. model->setData(index, combotext, Qt::EditRole);
    5. model->setData(index, combodata, Qt::UserRole);
    To copy to clipboard, switch view to plain text mode 
    it decided to behave like I would expect.

Similar Threads

  1. QTableView
    By dragon in forum Qt Programming
    Replies: 0
    Last Post: 22nd September 2008, 16:53
  2. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 08:30
  3. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

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.