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

运行图

相关推荐
plmm烟酒僧16 分钟前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Black_Friend1 小时前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC1 小时前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子2 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
苏三有春4 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
Vanranrr4 小时前
C++ QT
java·c++·qt
兆。4 小时前
掌握 PyQt5:从零开始的桌面应用开发
开发语言·爬虫·python·qt
徒步僧15 小时前
ThingsBoard规则链节点:RPC Call Reply节点详解
qt·microsoft·rpc
可峰科技16 小时前
斗破QT编程入门系列之一:认识Qt:初步使用(四星斗师)
开发语言·qt
我喜欢就喜欢17 小时前
基于qt vs下的视频播放
开发语言·qt·音视频