Results 1 to 3 of 3

Thread: QComboBox in QTreeView

  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default QComboBox in QTreeView

    Hey Everyone,
    I have a QTreeView that uses a QAbstractItemModel.
    I want Column 8.to use ComboBoxes, so I implemented a new delegate, but when compiling this error comes out: undefined reference to `ComboDelegate::ComboDelegate(QObject*)'

    Here is my code:

    Qt Code:
    1. /*TreeView*/
    2. ....
    3. #include <QtGui>
    4. #include "ComboDelegate.h"
    5. .....
    6. /*Error*/ ComboDelegate* delegate = new ComboDelegate();
    7. TreeView->setItemDelegateForColumn(8, delegate);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /*delegate.h*/
    2. ...
    3. class ComboDelegate : public QItemDelegate
    4. {
    5. Q_OBJECT
    6. public:
    7.  
    8. ComboDelegate(QObject *parent = 0);
    9. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    10. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    11. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    12. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    13. };
    14.  
    15. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /*delegate.cpp*/
    2. #include "ComboDelegate.h"
    3.  
    4. ComboDelegate::ComboDelegate(QObject *parent): QItemDelegate(parent)
    5. {
    6. }
    7.  
    8. QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
    9. {
    10. QComboBox *editor = new QComboBox(parent);
    11. QStringList list ;
    12. list << "a" << "b" << "c" << "d";
    13. editor->addItems(list);
    14. return editor;
    15. }
    16.  
    17. void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    18. {
    19. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    20. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    21. comboBox->addItem(value);
    22. }
    23.  
    24. void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    25. {
    26. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    27. QString value = comboBox->currentText();
    28. model->setData(index, value);
    29. }
    30.  
    31. void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    32. {
    33. editor->setGeometry(option.rect);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions ??

  2. #2
    Join Date
    Jul 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QComboBox in QTreeView

    it works, I just forgot an Include in the .pro.
    Last edited by mugi; 15th July 2011 at 18:22.

  3. #3
    Join Date
    Apr 2012
    Posts
    1
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QComboBox in QTreeView

    Hi,

    I know this thread is almost a year old but could you post a working example of how to load a Combobox into a TreeView?
    I'm quite new to the Qt World and I'm trying to do exactly what you did.

    Thanks in advance.

Similar Threads

  1. QComboBox and QTreeView
    By Carlsberg in forum Qt Programming
    Replies: 7
    Last Post: 14th February 2013, 16:58
  2. Replies: 3
    Last Post: 28th December 2010, 11:55
  3. problem with QTreeView and QComboBox
    By nirab_25 in forum Newbie
    Replies: 0
    Last Post: 22nd January 2010, 13:47
  4. QcomboBox update QTreeView
    By josiah47 in forum Newbie
    Replies: 2
    Last Post: 5th March 2009, 23:32
  5. How to use QTreeView in QComboBox?
    By Tamara in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 19:03

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.