【1】代理应用示例源码
用代码说事,比较靠谱。
代码目录:三个自定义类,重实现QStyledItemDelegate类。main函数应用示例。
(1)ComboDelegate.h
1 #ifndef COMBODELEGATE_H
2 #define COMBODELEGATE_H
3
4 #include <QStyledItemDelegate>
5
6 class ComboDelegate : public QStyledItemDelegate
7 {
8 public:
9 ComboDelegate(QObject *parent = NULL);
10
11 protected:
12 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
13 void setEditorData(QWidget *editor, const QModelIndex &index) const;
14 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
15 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
16 };
17
18 #endif // COMBODELEGATE_H
(2)ComboDelegate.cpp
1 #include "ComboDelegate.h"
2 #include <QComboBox>
3
4 ComboDelegate::ComboDelegate(QObject *parent)
5 : QStyledItemDelegate(parent)
6 {}
7
8 QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
9 {
10 Q_UNUSED(option);
11 Q_UNUSED(index);
12
13 QStringList list;
14 list << "工人" << "农民" << "军人" << "律师";
15
16 QComboBox *pEditor = new QComboBox(parent);
17 pEditor->addItems(list);
18 pEditor->installEventFilter(const_cast<ComboDelegate*>(this));
19 return pEditor;
20 }
21
22 void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
23 {
24 QString strText = index.model()->data(index).toString();
25 QComboBox *pCombox = NULL;
26 pCombox = static_cast<QComboBox*>(editor);
27 if (pCombox != NULL)
28 {
29 int nIndex = pCombox->findText(strText);
30 pCombox->setCurrentIndex(nIndex);
31 }
32 }
33
34 void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index)const
35 {
36 QComboBox *pCombox = NULL;
37 pCombox = static_cast<QComboBox*>(editor);
38 if (pCombox != NULL)
39 {
40 QString strText = pCombox->currentText();
41 model->setData(index, strText);
42 }
43 }
44
45 void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index)const
46 {
47 Q_UNUSED(index);
48 editor->setGeometry(option.rect);
49 }
(3)DateDelegate.h
1 #ifndef DATEDELEGATE_H
2 #define DATEDELEGATE_H
3
4 #include <QStyledItemDelegate>
5
6 class DateDelegate : public QStyledItemDelegate
7 {
8 public:
9 DateDelegate(QObject *parent = NULL);
10
11 protected:
12 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
13 void setEditorData(QWidget *editor, const QModelIndex &index) const;
14 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
15 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
16 };
17
18 #endif // DATEDELEGATE_H
(4)DateDelegate.cpp
1 #include "DateDelegate.h"
2 #include <QDateTimeEdit>
3
4 DateDelegate::DateDelegate(QObject *parent)
5 : QStyledItemDelegate(parent)
6 {}
7
8 // 首先创建要进行代理的窗体
9 QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
10 {
11 Q_UNUSED(option);
12 Q_UNUSED(index);
13
14 QDateTimeEdit *pEditor = new QDateTimeEdit(parent); // 一个日历的控件
15 pEditor->setDisplayFormat("yyyy-MM-dd"); // 日期时间的显示格式
16 pEditor->setCalendarPopup(true); // 以下拉的方式显示
17 pEditor->installEventFilter(const_cast<DateDelegate*>(this)); // 调用这个函数安装事件过滤器,使这个对象可以捕获QDateTimeEdit对象的事件
18 return pEditor;
19 }
20
21 // 这个是初始化作用,初始化代理控件的数据
22 void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
23 {
24 // 先用这个index返回这个model然后用这个model得到index对应的数据
25 QString strDate = index.model()->data(index).toString();
26 QDate date = QDate::fromString(strDate, Qt::ISODate); // 根据QString类型得到相应的时间类型
27 QDateTimeEdit *pEditor = NULL;
28 pEditor = static_cast<QDateTimeEdit*>(editor); // 强转为QDateTimeEdit*类型
29 if (pEditor != NULL)
30 {
31 pEditor->setDate(date); // 设置代理控件的显示数据
32 }
33 }
34
35 // 将代理控件里面的数据更新到视图控件中
36 // void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
37 void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
38 {
39 QDateTimeEdit *pEditor = NULL;
40 pEditor = static_cast<QDateTimeEdit*>(editor); // 得到时间
41 if (pEditor != NULL)
42 {
43 QDate date = pEditor->date(); // 得到时间
44 model->setData(index, QVariant(date.toString(Qt::ISODate))); // 把值放到相应的index里面
45 }
46 }
47
48 // 代理中数据的改变放到model中
49 // void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
50 void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
51 {
52 Q_UNUSED(index);
53 editor->setGeometry(option.rect);
54 }
(5)SpinDelegate.h
1 #ifndef SPINDELEGATE_H
2 #define SPINDELEGATE_H
3
4 #include <QStyledItemDelegate>
5
6 class SpinDelegate : public QStyledItemDelegate
7 {
8 public:
9 SpinDelegate(QObject *parent = NULL);
10
11 protected:
12 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
13 void setEditorData(QWidget *editor, const QModelIndex &index) const;
14 void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
15 void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
16 };
17
18 #endif // SPINDELEGATE_H
(6)SpinDelegate.cpp
1 #include "SpinDelegate.h"
2
3 #include <QSpinBox>
4
5 SpinDelegate::SpinDelegate(QObject *parent)
6 : QStyledItemDelegate(parent)
7 {
8 }
9
10 QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
11 {
12 Q_UNUSED(option);
13 Q_UNUSED(index);
14
15 QSpinBox *pEditor = new QSpinBox(parent);
16 pEditor->setRange(0, 30000);
17 pEditor->installEventFilter(const_cast<SpinDelegate*>(this));
18 return pEditor;
19 }
20
21 void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
22 {
23 int value = index.model()->data(index).toInt();
24 QSpinBox *pSpinbox = NULL;
25 pSpinbox = static_cast<QSpinBox*>(editor);
26 if (pSpinbox != NULL)
27 {
28 pSpinbox->setValue(value);
29 }
30 }
31
32 void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
33 {
34 QSpinBox *pSpinbox = NULL;
35 pSpinbox = static_cast<QSpinBox*>(editor);
36 if (pSpinbox != NULL)
37 {
38 int value = pSpinbox->value();
39 model->setData(index, value);
40 }
41 }
42
43 void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
44 {
45 Q_UNUSED(index);
46
47 editor->setGeometry(option.rect);
48 }
(7)main.cpp
1 #include <QApplication>
2 #include <QFile>
3 #include <QDebug>
4 #include <QWidget>
5 #include <QTableView>
6 #include <QTextStream>
7 #include <QStandardItemModel>
8 #include "DateDelegate.h"
9 #include "ComboDelegate.h"
10 #include "SpinDelegate.h"
11
12 int main(int argc, char *argv[])
13 {
14 QApplication a(argc, argv);
15
16 QStandardItemModel model(4, 4);
17 model.setHeaderData(0, Qt::Horizontal, QLatin1String("Name"));
18 model.setHeaderData(1, Qt::Horizontal, QLatin1String("Birthday"));
19 model.setHeaderData(2, Qt::Horizontal, QLatin1String("Job"));
20 model.setHeaderData(3, Qt::Horizontal, QLatin1String("Income"));
21
22 QFile file(QLatin1String("/mnt/liuy/info"));
23 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
24 {
25 qDebug() << "open the file failed...";
26 return -1;
27 }
28
29 QTextStream out(&file);
30 QString line;
31 model.removeRows(0, model.rowCount(QModelIndex()), QModelIndex());
32 int row = 0;
33 do
34 {
35 line = out.readLine();
36 if (!line.isEmpty())
37 {
38 model.insertRows(row, 1, QModelIndex());
39 QStringList pieces = line.split(",", QString::SkipEmptyParts);
40 model.setData(model.index(row, 0, QModelIndex()), pieces.value(0));
41 model.setData(model.index(row, 1, QModelIndex()), pieces.value(1));
42 model.setData(model.index(row, 2, QModelIndex()), pieces.value(2));
43 model.setData(model.index(row, 3, QModelIndex()), pieces.value(3));
44 ++row;
45 }
46 } while(!line.isEmpty());
47 file.close();
48
49 QTableView tableView;
50 tableView.setModel(&model); // 绑定Model
51 tableView.setWindowTitle(QLatin1String("Delegate"));
52
53 DateDelegate dateDelegate;
54 tableView.setItemDelegateForColumn(1, &dateDelegate); // 第一列代理
55 ComboDelegate comboDelegate;
56 tableView.setItemDelegateForColumn(2, &comboDelegate);// 第二列代理
57 SpinDelegate spinDelegate;
58 tableView.setItemDelegateForColumn(3, &spinDelegate); // 第三列代理
59
60 tableView.resize(500, 300); // 重置大小
61 tableView.show();
62
63 return a.exec();
64 }
备注:此示例运行环境为UBuntu + Qt5.3.2
【2】读取的信息文件
文件info 内容如下(注意:文件格式):
Liu,1977-01-05,工人,1500 Wang,1987-11-25,医生,2500 Sun,1967-10-05,军人,500 Zhang,1978-01-12,律师,4500
【3】运行效果图
运行效果图:
第二列编辑图(时间日期控件):
第三列编辑图(下拉框控件):
第四列编辑图(微调框控件):
Good Good Study, Day Day Up.
顺序 选择 循环 总结