Results 1 to 11 of 11

Thread: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableView.

  1. #1
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableView.

    Hello.

    Well the question sums it up. I have a data structure that has a list of pair values per object and I would like to show them in a QComboBox of a QTableView.

    Qt Code:
    1. typedef QPair<QString, QString> MyPair;
    2. typedef QList<MyPair> MyList;
    3. struct MyObject{
    4. MyList myList;
    5. };
    To copy to clipboard, switch view to plain text mode 

    How can I translate that into a QStandardItemModel?
    How can I render that in a QComboBox?

    I have already done it using a QTableWidget but maintaining both, the real data and the showed data, updated is getting messy.

    Thanks for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    There is no need to store a qlist of qpairs in a model. Just add rows and collumns

    Since a combobox can only show one collumn, you will need to create a delegate that combines two columns.

  3. #3
    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: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    You can simply set the text into combobox,, cant you ?
    Something like -
    comboBox->setItemText(index, QString("<%1 , %2 >").arg(myList.at(index).first).arg(myList.at(inde x).second));

    And can you explain how you used in table widget, it will help to understand better what you are trying to do.

  4. #4
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    @tbscope
    I got that. But in my case, since my object has this list of pairs, I need a way of storing this too. Its like some sort of inner table.

    @aamer4yu
    Yeah, I wouldn't mind rendering it that way. But how can I get a QStandardItem hold both Strings?

    Say you were showing this parsed xml in a QTableView:
    Qt Code:
    1. <item name="A">
    2. <texture name="active" file="red.jpg"/>
    3. <texture name="unactive" file="blue.jpg"/>
    4. </item>
    5. <item name="B">
    6. <texture name="ok" file="ok.jpg"/>
    7. <texture name="cancel" file="cancel.jpg"/>
    8. </item>
    To copy to clipboard, switch view to plain text mode 

    The table should show 2 rows, each row with a combo box showing the list of textures.
    Last edited by toglia3d; 15th June 2010 at 07:25.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Quote Originally Posted by toglia3d View Post
    I got that. But in my case, since my object has this list of pairs, I need a way of storing this too. Its like some sort of inner table.
    I understand. In theory you are working with two models that are combined in a complex way.
    Why don't you use the model directly in your object?

    However, you can add a qlist of qpairs to the model. Just use setData and a custom role.
    Beware though, you might need to register your custom type for QVariant to be able to work with it.

    If I would write this code, I would get rid of the qlist of qpairs and just use the model.

    In all cases, you need to create a delegate.

  6. #6
    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: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    I dont even see the need for table view...
    you can use layouts ... QLabels as item names, and QComboBox having list of textures.

    Combining combobox in tableview will not be easy,,, and as tbscope said, delegates will be needed.

  7. #7
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Quote Originally Posted by tbscope View Post
    I understand. In theory you are working with two models that are combined in a complex way.
    Why don't you use the model directly in your object?
    Sure, I was thinking on doing that. Maybe inheriting from QStandardItem to add the stuff I need.

    Quote Originally Posted by tbscope View Post
    In all cases, you need to create a delegate.
    Can you please point an example of this to use with combo boxes.

    Thanks.

  8. #8
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Quote Originally Posted by aamer4yu View Post
    I dont even see the need for table view...
    Well, I'm not only showing the items, other stuff is needed like adding/removing textures, changing the order of items, etc. I'm using the table to get the selected items and stuff.

    EDIT: My example showed two items... Real files have hundreds.

  9. #9
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Quote Originally Posted by toglia3d View Post
    Can you please point an example of this to use with combo boxes.
    It's with a spin box, but the technique is completely the same for a combo box too:
    http://doc.qt.nokia.com/4.6/itemview...xdelegate.html

    More examples:
    http://doc.qt.nokia.com/4.6/itemviews-stardelegate.html
    http://doc.qt.nokia.com/4.6/examples-itemviews.html

  10. #10
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Ok. Thanks. I managed to pull a simple demo.
    One thing to notice is that with:

    Qt Code:
    1. QModelIndex index = model.index(i,i, QModelIndex());
    2. tableView->openPersistentEditor(index);
    To copy to clipboard, switch view to plain text mode 

    It is possible to have the widget render by default.

    Beware though, you might need to register your custom type for QVariant to be able to work with it.
    What I wasn't aware was that I needed to retrieve my list of "textures" with a QModelIndex.data. So I think I'll need to do what you're saying.

    I'll seee that tommorrow.

  11. #11
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Storing QList of QPair in QStandardItem then rendering it in QComboBox of QTableV

    Well to make a final conclusion this is what needs to be done:

    1) Register the custom struct:
    Qt Code:
    1. struct MyObject {
    2. typedef QPair<QString, QString> MyPair;
    3. typedef QList<MyPair> MyList;
    4. MyList myList;
    5. };
    6. Q_DECLARE_METATYPE(MyObject)
    To copy to clipboard, switch view to plain text mode 

    2) Inserting items to the model:
    Qt Code:
    1. QModelIndex index = model->index(row, column);
    2. MyObject myObject;
    3. myObject.myList.push_back(
    4. MyObject::MyPair(QString("default"),QString("hello.png"))
    5. );
    6. myObject.myList.push_back(
    7. MyObject::MyPair(QString("active"),QString("world.png"))
    8. );
    9. model->setData(index, qVariantFromValue(myObject));
    10. tableView->openPersistentEditor(index);
    To copy to clipboard, switch view to plain text mode 

    3) Create the item delegate like the star delegate example:
    Qt Code:
    1. QWidget* MyDelegate::createEditor(
    2. QWidget* parent,
    3. const QStyleOptionViewItem& option,
    4. const QModelIndex& index) const
    5. {
    6. if(qVariantCanConvert<MyObject>(index.data())){
    7. QComboBox* editor = new QComboBox(parent);
    8. editor->setAutoFillBackground(true);
    9. return editor;
    10. }else{
    11. return QItemDelegate::createEditor(parent, option, index);
    12. }
    13. }
    14.  
    15. void MyDelegate::setEditorData(
    16. QWidget *editor,
    17. const QModelIndex &index) const
    18. {
    19. if(qVariantCanConvert<MyObject>(index.data())){
    20. QComboBox* comboBox = static_cast<QComboBox*>(editor);
    21. MyObject auxObject = qVariantValue<MyObject>(index.data());
    22. foreach(MyObject::MyPair par, auxObject.myList){
    23. comboBox->addItem(par.first+"\t"+par.first);
    24. }
    25. }else{
    26. return QItemDelegate::setEditorData(editor, index);
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    4) Set the item delegate to the QTableView:
    Qt Code:
    1. MyDelegate* myDelegate = new MyDelegate();
    2. tableView->setItemDelegate(myDelegate);
    To copy to clipboard, switch view to plain text mode 

    Thats it.

    But I have some last question, By default items that are not "MyObject" are editable, this is because I'm making them with QItemDelegate::createEditor I think. I have tried customizing this widget but I cant see a setEditable method. So, what is the real default widget of a QTableView item?
    Last edited by toglia3d; 20th June 2010 at 23:34.

Similar Threads

  1. Replies: 8
    Last Post: 7th November 2012, 20:20
  2. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 11:54
  3. QCombobox as QTableView delegate
    By martonmiklos in forum Qt Programming
    Replies: 7
    Last Post: 13th June 2010, 16:23
  4. Replies: 4
    Last Post: 12th October 2009, 20:36
  5. using QPair in QVector
    By damonlin in forum Qt Programming
    Replies: 5
    Last Post: 28th January 2009, 10:29

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.