Results 1 to 2 of 2

Thread: QComboBox in QTableView

  1. #1
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QComboBox in QTableView

    I need to show some cells in QTableView as QComboBox, even when the tableView no in edit mode. I create my class for it that inherits QStyledItemDelegateClass and reimplement paint method:
    Qt Code:
    1. //comboboxdelegate.h
    2. #ifndef COMBOBOXDELEGATE_H
    3. #define COMBOBOXDELEGATE_H
    4.  
    5. #include <QStyledItemDelegate>
    6. #include <QComboBox>
    7.  
    8. class ComboBoxDelegate : public QStyledItemDelegate
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. ComboBoxDelegate(QObject *parent = 0);
    14. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    15.  
    16. void setItems(const QStringList &itemsList);
    17.  
    18. private:
    19. QStringList m_itemsList;
    20. };
    21.  
    22. #endif // COMBOBOXDELEGATE_H
    23.  
    24. //comboboxdelegate.cpp
    25. #include "comboboxdelegate.h"
    26.  
    27. #include <QComboBox>
    28. #include <QApplication>
    29. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
    30. : QStyledItemDelegate(parent)
    31. {
    32. }
    33.  
    34. void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    35. {
    36. QString value = index.data().toString();
    37. QStyleOptionComboBox comboBoxOption;
    38. comboBoxOption.rect = option.rect;
    39. comboBoxOption.currentText = value;
    40. comboBoxOption.state = QStyle::State_Enabled;
    41. QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
    42. }
    To copy to clipboard, switch view to plain text mode 
    but comboBox is not showing in the cells, and data too.
    How can I draw the combobox in the cell?

  2. #2
    Join Date
    Nov 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox in QTableView

    Answer is
    Qt Code:
    1. ui->tableView->openPersistentEditor(m_model->index(0, 2));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  2. Replies: 10
    Last Post: 20th June 2010, 22:29
  3. QCombobox as QTableView delegate
    By martonmiklos in forum Qt Programming
    Replies: 7
    Last Post: 13th June 2010, 15:23
  4. Qcombobox
    By Project25 in forum Qt Programming
    Replies: 2
    Last Post: 16th December 2009, 19:29
  5. Replies: 2
    Last Post: 26th November 2009, 04:45

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.