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()
复制代码
{
复制代码
复制代码
}
复制代码
相关推荐
用户805533698033 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner3 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz8 天前
QML Hello World 入门示例
qt
xcyxiner11 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner12 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner12 天前
DicomViewer (添加模型类)3
qt
xcyxiner13 天前
DicomViewer (目录调整) 2
qt
xcyxiner13 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
桥田智能15 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构
森G15 天前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt