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()
复制代码
{
复制代码
复制代码
}
复制代码
相关推荐
薛定谔的猫喵喵13 分钟前
基于PyQt5的视频答题竞赛系统设计与实现
开发语言·qt·音视频
薛定谔的猫喵喵1 小时前
基于C++ Qt的唐代诗歌查询系统设计与实现
c++·qt·sqlite
枫叶丹42 小时前
【Qt开发】Qt界面优化(一)-> Qt样式表(QSS) 背景介绍
开发语言·前端·qt·系统架构
明月醉窗台14 小时前
qt使用笔记六之 Qt Creator、Qt Widgets、Qt Quick 详细解析
开发语言·笔记·qt
J_liaty14 小时前
23种设计模式一代理模式
设计模式·代理模式
R_.L17 小时前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
无小道19 小时前
Qt——常用控件
开发语言·qt
初次见面我叫泰隆19 小时前
Qt——5、Qt系统相关
开发语言·qt·客户端开发
牵牛老人21 小时前
【Qt 开发后台服务避坑指南:从库存管理系统开发出现的问题来看后台开发常见问题与解决方案】
开发语言·qt·系统架构
xmRao1 天前
Qt+FFmpeg 实现 PCM 音频转 AAC 编码
qt·ffmpeg·pcm