Qt26代理delegate

代理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();
}

运行图

相关推荐
徐霞客3204 小时前
Qt入门1——认识Qt的几个常用头文件和常用函数
开发语言·c++·笔记·qt
姆路5 小时前
QT Designer内存飙升
qt
Bruce小鬼7 小时前
QT文件基本操作
开发语言·qt
懷淰メ7 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
Mr.Q12 小时前
OpenCV和Qt坐标系不一致问题
qt·opencv
重生之我是数学王子15 小时前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
----云烟----1 天前
QT中QString类的各种使用
开发语言·qt
「QT(C++)开发工程师」1 天前
【qt版本概述】
开发语言·qt
一路冰雨1 天前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
老赵的博客1 天前
QT 自定义界面布局要诀
开发语言·qt