代理delegate
mainwindow
mainwindow.h
cpp
复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
#include <QTableView>
#include <QFile>
#include <QTextStream>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include "datedelegate.h"
#include "combodelegate.h"
#include "spindelegate.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
public:
void createAction();
void createMenu();
void setupModel();
void setupView();
void openFile(QString);
public slots:
void slotOpen();
private:
QMenu *fileMenu;
QAction* openAct;
QStandardItemModel* model;
QTableView *table;
DateDelegate* dataDelegate;
ComboDelegate* comboDelegate;
SpinDelegate* spinDelegate;
};
#endif // MAINWINDOW_H
mainwindow.cpp
cpp
复制代码
#include "mainwindow.h"
#include <QHeaderView>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QStringList>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createAction();
createMenu();
setupModel();
setupView();
setWindowTitle(tr("View Example"));
resize(600,600);
}
MainWindow::~MainWindow() {}
void MainWindow::createAction()
{
openAct = new QAction(tr("打开"),this);
connect(openAct,&QAction::triggered,this,&MainWindow::slotOpen);
}
void MainWindow::createMenu()
{
fileMenu = new QMenu("文件",this);
fileMenu->addAction(openAct);
menuBar()->addMenu(fileMenu);
}
void MainWindow::setupModel()
{
model = new QStandardItemModel(4,4,this);
model->setHeaderData(0,Qt::Horizontal,tr("姓名"));
model->setHeaderData(1,Qt::Horizontal,tr("生日"));
model->setHeaderData(2,Qt::Horizontal,tr("职业"));
model->setHeaderData(3,Qt::Horizontal,tr("收入"));
}
void MainWindow::setupView()
{
table = new QTableView;
table->setModel(model);
dataDelegate = new DateDelegate;
comboDelegate = new ComboDelegate;
spinDelegate = new SpinDelegate;
table->setItemDelegateForColumn(1,dataDelegate);
table->setItemDelegateForColumn(2,comboDelegate);
table->setItemDelegateForColumn(3,spinDelegate);
table->horizontalHeader()->setStyleSheet("QHeaderView::section { background-color: #FFCCCC; }");
setCentralWidget(table);
}
void MainWindow::openFile(QString path)
{
if(!path.isEmpty())
{
QFile file(path);
if(file.open(QFile::ReadOnly | QFile::Text))
{
QTextStream stream(&file);
stream.setCodec("UTF-8");
QString line;
model->removeRows(0,model->rowCount(QModelIndex()));//清除所有数据
int row = 0;
do
{
line = stream.readLine();
if(!line.isEmpty())
{
model->insertRows(row,1,QModelIndex());
QStringList pleces = line.split(",");
model->setData(model->index(row,0,QModelIndex()),pleces.value(0));
model->setData(model->index(row,1,QModelIndex()),pleces.value(1));
model->setData(model->index(row,2,QModelIndex()),pleces.value(2));
model->setData(model->index(row,3,QModelIndex()),pleces.value(3));
row++;
}
}while(!stream.atEnd());
file.close();
}
}
}
void MainWindow::slotOpen()
{
QString name;
name = QFileDialog::getOpenFileName(this,"打开",".","histogram files (*.txt)");
if(!name.isEmpty())
{
openFile(name);
}
}
datedelegate
datedelegate.h
cpp
复制代码
#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
datedelegate.cpp
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 dataStr = index.model()->data(index).toString();
QDate date = QDate::fromString(dataStr,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);
}
combodelegate
combodelegate.h
cpp
复制代码
#ifndef COMBODELEGATE_H
#define COMBODELEGATE_H
#include <QItemDelegate>
#include <QObject>
class ComboDelegate : public QItemDelegate
{
public:
explicit ComboDelegate(QObject *parent = nullptr);
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
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
spindelegate.h
cpp
复制代码
#ifndef SPINDELEGATE_H
#define SPINDELEGATE_H
#include <QItemDelegate>
#include <QObject>
class SpinDelegate : public QItemDelegate
{
public:
explicit SpinDelegate(QObject *parent = nullptr);
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
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* sbox = new QSpinBox(parent);
sbox->setRange(0,80000);
sbox->installEventFilter(const_cast<SpinDelegate*>(this));
return sbox;
}
void SpinDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
int value = index.model()->data(index).toInt();
QSpinBox* sbox = static_cast<QSpinBox*>(editor);
sbox->setValue(value);
}
void SpinDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QSpinBox* sbox = static_cast<QSpinBox*>(editor);
int value = sbox->value();
model->setData(index,value);
}
void SpinDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
main.cpp
cpp
复制代码
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
运行图