1、效果展示:
在表格中嵌入各种不同的控件,通过表格中的控件对编辑的内容进行限定。
2、代码部分
main.cpp
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"
#include <QApplication>
#include <QTableView>
#include <QFile>
#include <QTextStream>
#include <QStandardItemModel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStandardItemModel model(4,4);
QTableView tableView;
tableView.setModel(&model);
ComboDelegate comboDelegate;
SpinDelegate spinDelegate;
model.setHeaderData(0,Qt::Horizontal,QObject::tr("姓名"));
model.setHeaderData(1,Qt::Horizontal,QObject::tr("生日"));
model.setHeaderData(2,Qt::Horizontal,QObject::tr("职业"));
model.setHeaderData(3,Qt::Horizontal,QObject::tr("收入"));
QFile file("test.txt");
if(file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream stream;
QString line;
model.removeRows(0,model.rowCount(QModelIndex()),QModelIndex());
int row = 0;
do
{
line= stream.readLine();
if(!line.isEmpty())
{
model.insertRows(row,1,QModelIndex());
QStringList pieces = line.split(",",QString::SkipEmptyParts);
model.setData(model.index(row,0,QModelIndex()),pieces.value(0));
model.setData(model.index(row,1,QModelIndex()),pieces.value(1));
model.setData(model.index(row,2,QModelIndex()),pieces.value(2));
model.setData(model.index(row,3,QModelIndex()),pieces.value(3));
}
}while(!line.isEmpty());
file.close();
}
tableView.setWindowTitle(QObject::tr("Dalegate"));
tableView.show();
DateDelegate dateDelegate;
tableView.setItemDelegateForColumn(1,&dateDelegate);
tableView.setItemDelegateForColumn(2,&comboDelegate);
tableView.setItemDelegateForColumn(3,&spinDelegate);
// w.show();
return a.exec();
}
datedelegate.h
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H
#include <QItemDelegate>
class DateDelegate : public QItemDelegate
{
Q_OBJECT
public:
DateDelegate(QObject *parent = 0);
~DateDelegate();
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* eidtor,const QStyleOptionViewItem& option,const QModelIndex &index) const;
};
#endif // DATEDELEGATE_H
datedelegate.cpp
#include "datedelegate.h"
#include <QDateTimeEdit>
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);
}
DateDelegate::~DateDelegate()
{
}
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
combodelegate.cpp
#include "combodelegate.h"
#include <QComboBox>
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->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);
}
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
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,10000);
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);
}
全部代码了,如有写的不清楚的地方,欢迎在评论区给我留言