Difference between revisions of "Qt/Delegates/ComboBoxDelegate"
From ProgrammingExamples
< Qt
Daviddoria (Talk | contribs) (Created page with '==main.cpp== <source lang="cpp"> →main.cpp A simple example that shows how a view can use a custom delegate to edit data obtained from a model.: #include <QApplicatio…') |
Daviddoria (Talk | contribs) |
||
Line 1: | Line 1: | ||
==main.cpp== | ==main.cpp== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#include <QApplication> | #include <QApplication> | ||
#include <QHeaderView> | #include <QHeaderView> | ||
Line 37: | Line 30: | ||
{ | { | ||
QModelIndex index = model.index(row, column, QModelIndex()); | QModelIndex index = model.index(row, column, QModelIndex()); | ||
− | + | int value = (row+1) * (column+1); | |
+ | std::cout << "Setting (" << row << ", " << column << ") to " << value << std::endl; | ||
+ | model.setData(index, QVariant(value)); | ||
} | } | ||
+ | } | ||
+ | |||
+ | // Make the combo boxes always displayed. | ||
+ | for ( int i = 0; i < model.rowCount(); ++i ) | ||
+ | { | ||
+ | tableView.openPersistentEditor( model.index(i, 1) ); | ||
} | } | ||
Line 44: | Line 45: | ||
return app.exec(); | return app.exec(); | ||
} | } | ||
+ | |||
+ | |||
</source> | </source> | ||
Line 71: | Line 74: | ||
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; | void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; | ||
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; | void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; | ||
− | |||
private: | private: | ||
Line 121: | Line 123: | ||
{ | { | ||
QComboBox *comboBox = static_cast<QComboBox*>(editor); | QComboBox *comboBox = static_cast<QComboBox*>(editor); | ||
− | + | int value = index.model()->data(index, Qt::EditRole).toUInt(); | |
+ | comboBox->setCurrentIndex(value); | ||
} | } | ||
Line 134: | Line 137: | ||
editor->setGeometry(option.rect); | editor->setGeometry(option.rect); | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const | void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const |
Revision as of 15:13, 11 December 2011
main.cpp
#include <QApplication> #include <QHeaderView> #include <QItemSelectionModel> #include <QStandardItemModel> #include <QTableView> #include <iostream> #include "ComboBoxDelegate.h" int main(int argc, char *argv[]) { std::cout << "Enter." << std::endl; QApplication app(argc, argv); QStandardItemModel model(4, 2); QTableView tableView; tableView.setModel(&model); ComboBoxDelegate delegate; //tableView.setItemDelegate(&delegate); tableView.setItemDelegateForColumn(1, &delegate); // Column 0 can take any value, column 1 can only take values up to 8. for (int row = 0; row < 4; ++row) { for (int column = 0; column < 2; ++column) { QModelIndex index = model.index(row, column, QModelIndex()); int value = (row+1) * (column+1); std::cout << "Setting (" << row << ", " << column << ") to " << value << std::endl; model.setData(index, QVariant(value)); } } // Make the combo boxes always displayed. for ( int i = 0; i < model.rowCount(); ++i ) { tableView.openPersistentEditor( model.index(i, 1) ); } tableView.show(); return app.exec(); }
ComboBoxDelegate.h
#ifndef COMBOBOXDELEGATE_H #define COMBOBOXDELEGATE_H #include <string> #include <vector> #include <QItemDelegate> class QModelIndex; class QWidget; class QVariant; class ComboBoxDelegate : public QItemDelegate { Q_OBJECT public: ComboBoxDelegate(QObject *parent = 0); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const; void setEditorData(QWidget *editor, const QModelIndex &index) const; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const; void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; private: std::vector<std::string> Items; }; #endif
ComboBoxDelegate.cpp
#include "ComboBoxDelegate.h" #include <QComboBox> #include <QWidget> #include <QModelIndex> #include <QApplication> #include <QString> #include <iostream> ComboBoxDelegate::ComboBoxDelegate(QObject *parent) :QItemDelegate(parent) { Items.push_back("Test0"); Items.push_back("Test1"); Items.push_back("Test2"); Items.push_back("Test3"); Items.push_back("Test4"); Items.push_back("Test5"); Items.push_back("Test6"); Items.push_back("Test7"); Items.push_back("Test8"); } QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QComboBox* editor = new QComboBox(parent); for(unsigned int i = 0; i < Items.size(); ++i) { editor->addItem(Items[i].c_str()); } return editor; } void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QComboBox *comboBox = static_cast<QComboBox*>(editor); int value = index.model()->data(index, Qt::EditRole).toUInt(); comboBox->setCurrentIndex(value); } void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *comboBox = static_cast<QComboBox*>(editor); model->setData(index, comboBox->currentIndex(), Qt::EditRole); } void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { editor->setGeometry(option.rect); } void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItemV4 myOption = option; QString text = Items[index.row()].c_str(); myOption.text = text; QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter); }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(ComboBoxDelegate) FIND_PACKAGE(Qt4 REQUIRED) INCLUDE(${QT_USE_FILE}) QT4_WRAP_CPP(MOCSrcs ComboBoxDelegate.h) include_directories(${include_directories} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) ADD_EXECUTABLE(ComboBoxDelegate main.cpp ComboBoxDelegate.cpp ${MOCSrcs} ) TARGET_LINK_LIBRARIES(ComboBoxDelegate ${QT_LIBRARIES})