http://programmingexamples.net/w/index.php?title=Qt/ModelView/ListViewComboBox&feed=atom&action=history
Qt/ModelView/ListViewComboBox - Revision history
2024-03-29T05:45:34Z
Revision history for this page on the wiki
MediaWiki 1.23.5
http://programmingexamples.net/w/index.php?title=Qt/ModelView/ListViewComboBox&diff=5120&oldid=prev
Daviddoria: Created page with '==main.cpp== <source lang="cpp"> #include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); r…'
2011-12-21T02:21:10Z
<p>Created page with '==main.cpp== <source lang="cpp"> #include <QApplication> #include "form.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); Form form; form.show(); r…'</p>
<p><b>New page</b></p><div>==main.cpp==<br />
<source lang="cpp"><br />
#include <QApplication><br />
<br />
#include "form.h"<br />
<br />
int main(int argc, char *argv[])<br />
{<br />
QApplication app(argc, argv);<br />
Form form;<br />
<br />
form.show();<br />
return app.exec();<br />
}<br />
</source><br />
<br />
==ComboBoxDelegate.h==<br />
<source lang="cpp"><br />
#ifndef COMBOBOXDELEGATE_H<br />
#define COMBOBOXDELEGATE_H<br />
<br />
#include <string><br />
#include <vector><br />
<br />
#include <QItemDelegate><br />
<br />
class QModelIndex;<br />
class QWidget;<br />
class QVariant;<br />
<br />
class ComboBoxDelegate : public QItemDelegate<br />
{<br />
Q_OBJECT<br />
public:<br />
ComboBoxDelegate(QObject *parent = 0);<br />
<br />
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
void setEditorData(QWidget *editor, const QModelIndex &index) const;<br />
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;<br />
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;<br />
<br />
private:<br />
std::vector<std::string> Items;<br />
<br />
};<br />
#endif<br />
<br />
</source><br />
<br />
==ComboBoxDelegate.cpp==<br />
<source lang="cpp"><br />
#include "ComboBoxDelegate.h"<br />
<br />
#include <QComboBox><br />
#include <QWidget><br />
#include <QModelIndex><br />
#include <QApplication><br />
#include <QString><br />
<br />
#include <iostream><br />
<br />
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)<br />
:QItemDelegate(parent)<br />
{<br />
Items.push_back("Test0");<br />
Items.push_back("Test1");<br />
Items.push_back("Test2");<br />
}<br />
<br />
<br />
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const<br />
{<br />
QComboBox* editor = new QComboBox(parent);<br />
for(unsigned int i = 0; i < Items.size(); ++i)<br />
{<br />
editor->addItem(Items[i].c_str());<br />
}<br />
return editor;<br />
}<br />
<br />
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const<br />
{<br />
QComboBox *comboBox = static_cast<QComboBox*>(editor);<br />
int value = index.model()->data(index, Qt::EditRole).toUInt();<br />
comboBox->setCurrentIndex(value);<br />
}<br />
<br />
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const<br />
{<br />
QComboBox *comboBox = static_cast<QComboBox*>(editor);<br />
model->setData(index, comboBox->currentIndex(), Qt::EditRole);<br />
}<br />
<br />
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const<br />
{<br />
editor->setGeometry(option.rect);<br />
}<br />
<br />
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const<br />
{<br />
QStyleOptionViewItemV4 myOption = option;<br />
QString text = Items[index.row()].c_str();<br />
<br />
myOption.text = text;<br />
<br />
QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);<br />
}<br />
<br />
</source><br />
<br />
==form.h==<br />
<source lang="cpp"><br />
#ifndef FORM_H<br />
#define FORM_H<br />
<br />
#include "ui_form.h"<br />
<br />
#include <QMainWindow><br />
<br />
#include "ComboBoxDelegate.h"<br />
<br />
class QStringListModel;<br />
<br />
class Form : public QMainWindow, private Ui::MainWindow<br />
{<br />
Q_OBJECT<br />
<br />
public:<br />
Form(QWidget *parent = 0);<br />
<br />
public slots:<br />
void on_btnAdd_clicked();<br />
void slot_modelChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);<br />
<br />
protected:<br />
QStringListModel* Model;<br />
ComboBoxDelegate Delegate;<br />
};<br />
<br />
#endif<br />
<br />
</source><br />
<br />
==form.cpp==<br />
<source lang="cpp"><br />
#include <QtGui><br />
<br />
#include <iostream><br />
<br />
#include "form.h"<br />
<br />
Form::Form(QWidget *parent)<br />
: QMainWindow(parent)<br />
{<br />
setupUi(this);<br />
<br />
this->Model = new QStringListModel;<br />
this->listView->setModel(this->Model);<br />
connect(this->Model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(slot_modelChanged(QModelIndex,QModelIndex)));<br />
<br />
this->listView->setItemDelegate(&this->Delegate);<br />
}<br />
<br />
void Form::on_btnAdd_clicked()<br />
{<br />
this->Model->insertRows(this->Model->rowCount(), 1);<br />
this->Model->setData(this->Model->index(this->Model->rowCount()-1), "new line " + QString::number(this->Model->rowCount()));<br />
}<br />
<br />
void Form::slot_modelChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)<br />
{<br />
std::cout << "Model changed." << std::endl;<br />
<br />
// Make the combo boxes always displayed.<br />
for ( int i = 0; i < this->Model->rowCount(); ++i )<br />
{<br />
this->listView->openPersistentEditor( this->Model->index(i) );<br />
}<br />
}<br />
<br />
</source><br />
<br />
==form.ui==<br />
<source lang="text"><br />
<?xml version="1.0" encoding="UTF-8"?><br />
<ui version="4.0"><br />
<class>MainWindow</class><br />
<widget class="QMainWindow" name="MainWindow"><br />
<property name="geometry"><br />
<rect><br />
<x>0</x><br />
<y>0</y><br />
<width>232</width><br />
<height>489</height><br />
</rect><br />
</property><br />
<property name="windowTitle"><br />
<string>MainWindow</string><br />
</property><br />
<widget class="QWidget" name="centralwidget"><br />
<layout class="QVBoxLayout" name="verticalLayout"><br />
<item><br />
<widget class="QListView" name="listView"/><br />
</item><br />
<item><br />
<widget class="QPushButton" name="btnAdd"><br />
<property name="text"><br />
<string>Add</string><br />
</property><br />
</widget><br />
</item><br />
</layout><br />
</widget><br />
<widget class="QMenuBar" name="menubar"><br />
<property name="geometry"><br />
<rect><br />
<x>0</x><br />
<y>0</y><br />
<width>232</width><br />
<height>20</height><br />
</rect><br />
</property><br />
<widget class="QMenu" name="menuFile"><br />
<property name="title"><br />
<string>File</string><br />
</property><br />
<addaction name="actionSave"/><br />
</widget><br />
<addaction name="menuFile"/><br />
</widget><br />
<widget class="QStatusBar" name="statusBar"/><br />
<action name="actionSave"><br />
<property name="text"><br />
<string>Save</string><br />
</property><br />
</action><br />
</widget><br />
<resources/><br />
<connections/><br />
</ui><br />
<br />
</source><br />
<br />
==CMakeLists.txt==<br />
<source lang="cmake"><br />
cmake_minimum_required(VERSION 2.6)<br />
<br />
PROJECT(ListViewSubclass)<br />
<br />
FIND_PACKAGE(Qt4 REQUIRED)<br />
INCLUDE(${QT_USE_FILE})<br />
<br />
QT4_WRAP_UI(ListViewSubclass_UI form.ui)<br />
QT4_WRAP_CPP(ListViewSubclass_MOC form.h ComboBoxDelegate.h)<br />
<br />
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})<br />
<br />
ADD_EXECUTABLE(ListViewSubclass main.cpp form.h form.cpp ComboBoxDelegate.cpp ${ListViewSubclass_UI} ${ListViewSubclass_MOC})<br />
TARGET_LINK_LIBRARIES(ListViewSubclass ${QT_LIBRARIES})<br />
<br />
</source></div>
Daviddoria