当前位置: 首页> 娱乐> 八卦 > 域名交易平台哪个好_模板下载网站源码_优化设计官网_微信群免费推广平台

域名交易平台哪个好_模板下载网站源码_优化设计官网_微信群免费推广平台

时间:2025/7/10 11:06:32来源:https://blog.csdn.net/oracleworm/article/details/146849128 浏览次数:1次
域名交易平台哪个好_模板下载网站源码_优化设计官网_微信群免费推广平台

// 头文件.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()
{
 
}
关键字:域名交易平台哪个好_模板下载网站源码_优化设计官网_微信群免费推广平台

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: