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()
复制代码
{
复制代码
复制代码
}
复制代码
相关推荐
YoseZang2 小时前
【设计模式】GoF设计模式之代理模式(Proxy Pattern)
设计模式·代理模式
不断努力的根号七8 小时前
qt框架,使用webEngine如何调试前端
开发语言·前端·qt
范纹杉想快点毕业17 小时前
基于C语言的Zynq SOC FPGA嵌入式裸机设计和开发教程
c语言·开发语言·数据库·嵌入式硬件·qt·fpga开发·嵌入式实时数据库
程序员编程指南17 小时前
Qt容器类:QList、QMap等的高效使用
c语言·开发语言·c++·qt
程序员编程指南18 小时前
Qt 元对象系统(Meta-Object System)解析
c语言·开发语言·c++·qt
XXYBMOOO19 小时前
使用全局变量访问 Qt UI 组件的方法文档
c++·qt·ui
kaikaile199521 小时前
基于Qt的仿QQ聊天系统设计
开发语言·qt
钱彬 (Qian Bin)1 天前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——4. 前后端联动:打通QML与C++的任督二脉
c++·qt·教程·qml·qt quick·qt 6.9.1·工业瑕疵检测
进击ing小白1 天前
Qt WebEngine Widgets的使用
开发语言·qt
QUST-Learn3D1 天前
Qt自定义图像显示控件(支持平移、缩放、横纵比自适应)
qt