Results 1 to 8 of 8

Thread: Add QComboBox in a QTreeView with QAbstractItemModel

  1. #1
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Question Add QComboBox in a QTreeView with QAbstractItemModel

    Hello;

    I have a QTreeView using a model subclassing QAbstractItemModel.

    I want one of the item (QModelIndex) to be QComboBox?

    Thank you for any help.

    Mut.

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

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    Implement a custom delegate that will return a combo box for these items.
    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.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    If you want a cell editable as a combobox, then you need an item delegate that creates a combobox as the editor for that cell.

    See QStyledItemDelegate for an often suitable base class.

    Cheers,
    _

  4. #4
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    Thank you for the reply.

    This thread http://www.qtcentre.org/threads/4314...x-in-QTreeView shows how to implement a delegate.
    However, I'm wondering if "TreeView->setItemDelegateForColumn(8, delegate);" will not put a combobox in all the cells of the column?

    What is the API the put the combobox into a specicifc QModelIndex?

    I wonder if there is a clean example out there with not only how to create a delegate but also how to use it with QtreeView-QAstractItemModel?

    Mut.

  5. #5
    Join Date
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    QComboBox *cb = new QComboBox;
    cb->addItem("a");
    treeView->setIndexWidget(currentIndex,cb);

    I have used above code and its seems to work. what is the difference between the delegate approach and the setIndexWidget() approach?

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

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    setIndexWidget does not bind the widget with the model in any way.
    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
    Apr 2014
    Posts
    125
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Android Maemo/MeeGo

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    Thanks for the reply.
    I'm using signal/slot to connect with the model. It is working fine.
    How to delete a combobox that was added with setIndexWidget()?
    Use case is: 1) the row holding the combobox is deleted 2) the combobox needs to be relocated to another location due to tree layout change

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Add QComboBox in a QTreeView with QAbstractItemModel

    Quote Originally Posted by mut View Post
    However, I'm wondering if "TreeView->setItemDelegateForColumn(8, delegate);" will not put a combobox in all the cells of the column?
    Yes, but the delegate can easily decide not to do its special behavior for a given model index, i.e. just call the base implementation for cells it doesn't feel responsible for.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 6th December 2012, 19:56
  2. QTreeView with QAbstractItemModel
    By Fletcher in forum Qt Programming
    Replies: 4
    Last Post: 10th December 2009, 23:02
  3. Creating a QAbstractItemModel for QTreeView
    By hbill in forum Qt Programming
    Replies: 13
    Last Post: 14th August 2008, 16:01
  4. Need help with QTreeView/QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th August 2006, 15:09
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 20:16

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.