Qt 代理

发布于:2025-04-03 ⋅ 阅读:(18) ⋅ 点赞:(0)

// 头文件.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()
{

}

网站公告

今日签到

点亮在社区的每一天
去签到