Qt 代理

// 头文件.h

复制代码
#ifndef SPINBOXDELEGATE_H
复制代码
#define SPINBOXDELEGATE_H
复制代码
复制代码
复制代码
复制代码
#include <QItemDelegate>
复制代码
复制代码
#include <QObject>
复制代码
#include <QStyledItemDelegate>
复制代码
复制代码
class SpinBoxDelegate : public QItemDelegate
复制代码
{
复制代码
    Q_OBJECT
复制代码
public:
复制代码
    SpinBoxDelegate(QObject* parent = 0);
复制代码
复制代码
    // editing
复制代码
    QWidget *createEditor(QWidget *parent,
复制代码
                          const QStyleOptionViewItem &option,
复制代码
                          const QModelIndex &index) const override;
复制代码
复制代码
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
复制代码
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
复制代码
复制代码
    void updateEditorGeometry(QWidget *editor,
复制代码
                              const QStyleOptionViewItem &option,
复制代码
                              const QModelIndex &index) const override;
复制代码
复制代码
};
复制代码
复制代码
class ComboBoxDelegate : public QItemDelegate
复制代码
{
复制代码
    Q_OBJECT
复制代码
public:
复制代码
    ComboBoxDelegate(QObject* parent = 0);
复制代码
复制代码
    // editing
复制代码
    QWidget *createEditor(QWidget *parent,
复制代码
                          const QStyleOptionViewItem &option,
复制代码
                          const QModelIndex &index) const override;
复制代码
复制代码
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
复制代码
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
复制代码
复制代码
    void updateEditorGeometry(QWidget *editor,
复制代码
                              const QStyleOptionViewItem &option,
复制代码
                              const QModelIndex &index) const override;
复制代码
};
复制代码
复制代码
class ProgressBarDelegate : public QStyledItemDelegate
复制代码
{
复制代码
    Q_OBJECT
复制代码
public:
复制代码
    ProgressBarDelegate(QObject* parent);
复制代码
复制代码
protected:
复制代码
    // painting
复制代码
    void paint(QPainter *painter,
复制代码
               const QStyleOptionViewItem &option, const QModelIndex &index) const override;
复制代码
复制代码
};
复制代码
复制代码
复制代码
#endif // SPINBOXDELEGATE_H
复制代码
// 源文件
复制代码
#include "spinboxdelegate.h"
复制代码
#include <QComboBox>
复制代码
#include <QSpinBox>
复制代码
#include <QApplication>
复制代码
复制代码
SpinBoxDelegate::SpinBoxDelegate(QObject* parent)
复制代码
    :QItemDelegate (parent)
复制代码
{
复制代码
复制代码
}
复制代码
复制代码
QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
复制代码
{
复制代码
    QSpinBox * editor = new QSpinBox(parent);
复制代码
    editor->setMinimum(0);
复制代码
    editor->setMaximum(100);
复制代码
    return editor;
复制代码
}
复制代码
复制代码
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
复制代码
{
复制代码
    int value = index.model()->data(index, Qt::EditRole).toInt();
复制代码
    auto * spinBox = static_cast<QSpinBox*>(editor);
复制代码
    spinBox->setValue(value);
复制代码
}
复制代码
复制代码
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
复制代码
{
复制代码
    auto* spinBox = static_cast<QSpinBox*>(editor);
复制代码
    spinBox->interpretText();   // 刷新
复制代码
    int value = spinBox->value();
复制代码
    model->setData(index, value, Qt::EditRole);
复制代码
}
复制代码
复制代码
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
复制代码
{
复制代码
    editor->setGeometry(option.rect);
复制代码
}
复制代码
复制代码
复制代码
///
复制代码
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
复制代码
{
复制代码
复制代码
}
复制代码
复制代码
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
复制代码
{
复制代码
    QComboBox* editor = new QComboBox(parent);
复制代码
    editor->addItem("Student 1", 1);
复制代码
    editor->addItem("Student 2", 2);
复制代码
    return editor;
复制代码
}
复制代码
复制代码
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
复制代码
{
复制代码
    int value = index.model()->data(index, Qt::EditRole).toInt() % 2;
复制代码
    QComboBox* comboBox = static_cast<QComboBox*>(editor);
复制代码
    comboBox->setCurrentIndex(value);
复制代码
}
复制代码
复制代码
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
复制代码
{
复制代码
    QComboBox* comboBox = static_cast<QComboBox*>(editor);
复制代码
    int value = comboBox->currentIndex();
复制代码
    model->setData(index, value, Qt::EditRole);
复制代码
}
复制代码
复制代码
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
复制代码
{
复制代码
    editor->setGeometry(option.rect);
复制代码
}
复制代码
复制代码
复制代码
///
复制代码
ProgressBarDelegate::ProgressBarDelegate(QObject* parent)
复制代码
{
复制代码
复制代码
}
复制代码
复制代码
// 绘制进度条
复制代码
void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
复制代码
{
复制代码
    if (index.isValid() && index.column() == 1){
复制代码
        QStyleOptionProgressBar bar;
复制代码
        bar.rect = option.rect;
复制代码
        bar.progress = index.data().toInt();
复制代码
        bar.maximum = 100;
复制代码
        bar.minimum = 0;
复制代码
        bar.text = QString::number(bar.progress) + "%";
复制代码
        bar.textVisible = true;
复制代码
复制代码
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &bar, painter);
复制代码
    }else{
复制代码
        QStyledItemDelegate::paint(painter, option, index);
复制代码
    }
复制代码
}
复制代码

// 使用

复制代码
#include "mainwindow.h"
复制代码
#include "ui_mainwindow.h"
复制代码
复制代码
#include <QStandardItemModel>
复制代码
#include <QTableView>
复制代码
#include "spinboxdelegate.h"
复制代码
复制代码
#include <QTimer>
复制代码
复制代码
复制代码
MainWindow::MainWindow(QWidget *parent) :
复制代码
    QMainWindow(parent),
复制代码
    ui(new Ui::MainWindow)
复制代码
{
复制代码
    ui->setupUi(this);
复制代码
复制代码
    auto* model = new QStandardItemModel(7, 4, this);
复制代码
    for (int row = 0; row < 7; row++){
复制代码
        for (int column = 0; column < 4; column++){
复制代码
            QStandardItem* item = new QStandardItem(QString("%1").arg(row*4 + column));
复制代码
            model->setItem(row, column, item);
复制代码
        }
复制代码
    }
复制代码
复制代码
    QTableView* _tableView = new QTableView;
复制代码
    _tableView->setModel(model);
复制代码
    setCentralWidget(_tableView);
复制代码
    this->resize(800, 800);
复制代码
复制代码
复制代码
    auto* model2 = new QStandardItemModel(7, 4, this);
复制代码
    for (int row = 0; row < 7; row++){
复制代码
        for (int column = 0; column < 4; column++){
复制代码
            QStandardItem* item = new QStandardItem(QString("%1").arg(row*4 + column));
复制代码
            model2->setItem(row, column, item);
复制代码
        }
复制代码
    }
复制代码
复制代码
    auto* delegate = new SpinBoxDelegate(this);
复制代码
    _tableView2 = new QTableView();
复制代码
    _tableView2->setModel(model2);
复制代码
    _tableView2->setItemDelegateForColumn(0, delegate);
复制代码
    auto* delegateCombo = new ComboBoxDelegate(this);
复制代码
    _tableView2->setItemDelegateForColumn(3, delegateCombo);
复制代码
    auto* progressBarDelegate = new ProgressBarDelegate(this);
复制代码
    _tableView2->setItemDelegateForColumn(1, progressBarDelegate);
复制代码
    _tableView2->show();
复制代码
    _tableView2->resize(800, 800);
复制代码
复制代码
    timer = new QTimer();
复制代码
    connect(timer, &QTimer::timeout, this, &MainWindow::slot_timeout);
复制代码
    timer->setInterval(1000);
复制代码
    timer->start();
复制代码
}
复制代码
复制代码
MainWindow::~MainWindow()
复制代码
{
复制代码
    delete ui;
复制代码
复制代码
    delete _tableView2;
复制代码
}
复制代码
复制代码
void MainWindow::slot_timeout()
复制代码
{
复制代码
复制代码
}
复制代码
相关推荐
@hdd4 小时前
Qt实现HTTP GET/POST/PUT/DELETE请求
qt·http
阳光_你好6 小时前
详细说明Qt 中共享内存方法: QSharedMemory 对象
开发语言·数据库·qt
爱吃馒头爱吃鱼6 小时前
QML编程中的性能优化二
开发语言·qt·学习·性能优化
m0_555762908 小时前
qml 基本元素
qt·qml
yuanbenshidiaos8 小时前
面试问题总结:qt工程师/c++工程师
c++·qt·面试
秋风&萧瑟9 小时前
【QT】QT的多界面跳转以及界面之间传递参数
开发语言·qt
AAA废品回收站陈师傅12 小时前
10乱码问题的解释(1)
qt
长流小哥12 小时前
可视化开发:用Qt实现Excel级动态柱状图
开发语言·c++·qt·ui
嘤国大力士15 小时前
C++11&QT复习 (十)
java·c++·qt
only-lucky15 小时前
QT之QML(简单示例)
前端·javascript·qt