Results 1 to 3 of 3

Thread: QCombobox in QTreeview

  1. #1
    Join Date
    Oct 2013
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    2

    Default QCombobox in QTreeview

    ok so i'm trying to get a combobox to show up in my tree view, but for some reason it won't allow me to edit it, my view select thing is set for row select.

    Also is there a way i can edit an item while hovering over it, so e.g i hover over an item with a combobox and just click the box and it comes up with options.

    Qt Code:
    1. inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QStyleOptionComboBox comboBoxOption;
    4. comboBoxOption.rect = option.rect;
    5. comboBoxOption.state = QStyle::State_Enabled;
    6. comboBoxOption.frame = true;
    7. comboBoxOption.currentText = index.data().toString();
    8. QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
    9. //QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
    10.  
    11. }
    12.  
    13. inline QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    14. {
    15. qDebug() << "Editor created";
    16. // ComboBox ony in column 2
    17.  
    18. // Create the combobox and populate it
    19. QComboBox *cb = new QComboBox(parent);
    20. int row = index.row();
    21. cb->addItem(QString("one in row %1").arg(row));
    22. cb->addItem(QString("two in row %1").arg(row));
    23. cb->addItem(QString("three in row %1").arg(row));
    24. return cb;
    25. }
    26.  
    27. inline void setEditorData ( QWidget *editor, const QModelIndex &index ) const
    28. {
    29. if(QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
    30. // get the index of the text in the combobox that matches the current value of the itenm
    31. QString currentText = index.data(Qt::EditRole).toString();
    32. int cbIndex = cb->findText(currentText);
    33. // if it is valid, adjust the combobox
    34. if(cbIndex >= 0)
    35. cb->setCurrentIndex(cbIndex);
    36. } else {
    37. QStyledItemDelegate::setEditorData(editor, index);
    38. }
    39. }
    40. inline void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    41. {
    42. if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
    43. // save the current text of the combo box as the current value of the item
    44. model->setData(index, cb->currentText(), Qt::EditRole);
    45. else
    46. QStyledItemDelegate::setModelData(editor, model, index);
    47. }
    48. inline void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    49. {
    50. editor->setGeometry(option.rect);
    51. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QCombobox in QTreeview

    Do you want to show the QComboBox all the time or just while editing the cell data?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Oct 2013
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    2

    Default Re: QCombobox in QTreeview

    i want to show the combobox when the mouse is hovering over the row of the tree view

Similar Threads

  1. QComboBox and QTreeView
    By Carlsberg in forum Qt Programming
    Replies: 7
    Last Post: 14th February 2013, 17:58
  2. QComboBox in QTreeView
    By mugi in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2012, 11:04
  3. QComboBox in QTreeView on Edit?
    By AyaKoshigaya in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2012, 19:16
  4. QcomboBox update QTreeView
    By josiah47 in forum Newbie
    Replies: 2
    Last Post: 6th March 2009, 00:32
  5. How to use QTreeView in QComboBox?
    By Tamara in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2007, 20: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
  •  
Qt is a trademark of The Qt Company.