關鍵是重寫這四個函數:
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;
程序運行截圖如下:
代碼如下:
combodelegate.h
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H#include <QItemDelegate>class ComboDelegate:public QItemDelegate
{Q_OBJECT
public:ComboDelegate(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;
};#endif // COMBODELEGATE_H
datedelegate.h
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H#include <QItemDelegate>class DateDelegate:public QItemDelegate
{Q_OBJECT
public:DateDelegate(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;
};#endif // DATEDELEGATE_H
passportdelegate.h
#ifndef PASSPORTDELEGATE_H
#define PASSPORTDELEGATE_H#include <QItemDelegate>
#include <QLabel>class PassportDelegate:public QItemDelegate
{Q_OBJECT
public:PassportDelegate(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;
};#endif // PASSPORTDELEGATE_H
spindelegate.h
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H#include <QItemDelegate>class SpinDelegate:public QItemDelegate
{Q_OBJECT
public:SpinDelegate(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;
};#endif // SPINDELEGATE_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>namespace Ui {
class Widget;
}class Widget : public QWidget
{Q_OBJECTpublic:explicit Widget(QWidget *parent = 0);~Widget();private:Ui::Widget *ui;
};#endif // WIDGET_H
combodelegate.cpp
#include "combodelegate.h"
#include <QComboBox>
#include <QDebug>ComboDelegate::ComboDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QComboBox *editor=new QComboBox(parent);editor->addItem("程序員");editor->addItem("網管");editor->addItem("修電腦的");editor->addItem("送水的");editor->installEventFilter(const_cast<ComboDelegate*>(this));return editor;
}void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString str=index.model()->data(index).toString();QComboBox *box=static_cast<QComboBox*>(editor);int i=box->findText(str);box->setCurrentIndex(i);
}void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QComboBox *box=static_cast<QComboBox*>(editor);QString str=box->currentText();model->setData(index,str);
}void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}
datedelegate.cpp
#include "datedelegate.h"
#include <QDateTimeEdit>
#include <QDebug>DateDelegate::DateDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QDateTimeEdit *editor=new QDateTimeEdit(parent);editor->setDisplayFormat("yyyy-MM-dd");editor->setCalendarPopup(true);editor->installEventFilter(const_cast<DateDelegate*>(this));return editor;
}void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString dateStr=index.model()->data(index).toString();QDate date=QDate::fromString(dateStr,Qt::ISODate);QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);edit->setDate(date);
}void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QDateTimeEdit *edit=static_cast<QDateTimeEdit*>(editor);QDate date=edit->date();model->setData(index,QVariant(date.toString(Qt::ISODate)));
}void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}
main.cpp
#include "widget.h"
#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}
passportdelegate.cpp
#include "passportdelegate.h"
#include <QCheckBox>
#include <QDebug>
#include <QFont>PassportDelegate::PassportDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *PassportDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QCheckBox *checkBox=new QCheckBox(parent);checkBox->setText("是否通過");QFont font;font.setBold(true);font.setPixelSize(18);checkBox->setFont(font);return checkBox;
}void PassportDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{QString str=index.model()->data(index).toString();QCheckBox *checkBox=static_cast<QCheckBox*>(editor);if(str=="通過"){checkBox->setChecked(Qt::Checked);}else{checkBox->setChecked(Qt::Unchecked);}
}void PassportDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QCheckBox *checkBox=static_cast<QCheckBox*>(editor);if(checkBox->isChecked()){model->setData(index,QString("通過"));}else{model->setData(index,QString("不通過"));}
}void PassportDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}
spindelegate.cpp
#include "spindelegate.h"
#include <QSpinBox>SpinDelegate::SpinDelegate(QObject *parent):QItemDelegate(parent)
{}QWidget *SpinDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QSpinBox *editor=new QSpinBox(parent);editor->setRange(0,100000);editor->installEventFilter(const_cast<SpinDelegate*>(this));return editor;}void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{int value=index.model()->data(index).toInt();QSpinBox *box=static_cast<QSpinBox*>(editor);box->setValue(value);
}void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QSpinBox *box=static_cast<QSpinBox*>(editor);int value=box->value();model->setData(index,value);
}void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "combodelegate.h"
#include "datedelegate.h"
#include "spindelegate.h"
#include "passportdelegate.h"
#include <QStandardItem>
#include <QStandardItemModel>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget)
{ui->setupUi(this);QStandardItemModel *model=new QStandardItemModel;model->setColumnCount(5);ComboDelegate *comboDelegate=new ComboDelegate;DateDelegate *dateDelegate=new DateDelegate;SpinDelegate *spinDelegate=new SpinDelegate;PassportDelegate *passportDelegate=new PassportDelegate;model->setHeaderData(0,Qt::Horizontal,"姓名");model->setHeaderData(1,Qt::Horizontal,"職業");model->setHeaderData(2,Qt::Horizontal,"出生日期");model->setHeaderData(3,Qt::Horizontal,"薪資");model->setHeaderData(4,Qt::Horizontal,"審核狀態");model->insertRow(0,new QStandardItem("豬小明"));ui->tableView->setModel(model);ui->tableView->setItemDelegateForColumn(1,comboDelegate);ui->tableView->setItemDelegateForColumn(2,dateDelegate);ui->tableView->setItemDelegateForColumn(3,spinDelegate);ui->tableView->setItemDelegateForColumn(4,passportDelegate);
}Widget::~Widget()
{delete ui;
}
總結
以上是生活随笔為你收集整理的Qt工作笔记-对QItemDelegate自定义委托的理解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。