本文主要是介绍QStyledItemDelegate自定义代理组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、继承QStyledItemDelegate自定义类组件
2、重写函数
3、通过在QTableView中插入自定义代理组件为例
继承QStyledItemDelegate写的头文件
#ifndef CUSTOMPUSHBOTTONDELEGATE_H
#define CUSTOMPUSHBOTTONDELEGATE_H#include <QObject>
#include <QStyledItemDelegate>
#include <QPushButton>
class CustomPushBottonDelegate : public QStyledItemDelegate
{Q_OBJECTpublic:CustomPushBottonDelegate();~CustomPushBottonDelegate();QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;void setEditorData(QWidget *editor, const QModelIndex &index) const;void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;private:};#endif // CUSTOMPUSHBOTTONDELEGATE_H
函数实现
#include "customspinboxdelegate.h"CustomSpinBoxDelegate::CustomSpinBoxDelegate()
{
}CustomSpinBoxDelegate::~CustomSpinBoxDelegate()
{}//创建代理编辑组件
QWidget* CustomSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QSpinBox* editor = new QSpinBox(parent);editor->setFrame(false);editor->setMinimum(0);editor->setMaximum(100000);return editor;
}//从数据模型获取数据,显示到代理组件中
void CustomSpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{int value = index.model()->data(index, Qt::EditRole).toInt();QSpinBox* spinBox = static_cast<QSpinBox*>(editor);spinBox->setValue(value);
}//将代理组件的数据保存到数据模型中
void CustomSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{QSpinBox* spinBox = static_cast<QSpinBox*>(editor);spinBox->interpretText();int value = spinBox->value();model->setData(index, value, Qt::EditRole);
}//设置组件大小
void CustomSpinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{editor->setGeometry(option.rect);
}
main中过程
头文件
#ifndef QVARIANTCUSTOM_H
#define QVARIANTCUSTOM_H#include <QtWidgets/QWidget>
#include "ui_qvariantcustom.h"
#include <QDebug>
#include "customspinboxdelegate.h"
#include <QStandardItemModel>
#include "customcomboboxdelegate.h"
#include "custompushbottondelegate.h"class QVariantCustom : public QWidget
{Q_OBJECTpublic:QVariantCustom(QWidget *parent = 0);~QVariantCustom();QStandardItemModel* pModel = nullptr;
private:Ui::QVariantCustomClass ui;CustomSpinBoxDelegate spinBoxDelegate;CustomComboBoxDelegate comboBoxDelegate;CustomPushBottonDelegate pushButtonDelegate;
};#endif // QVARIANTCUSTOM_H
主函数
#include "qvariantcustom.h"QVariantCustom::QVariantCustom(QWidget *parent): QWidget(parent)
{ui.setupUi(this);pModel = new QStandardItemModel(2, 3, this);ui.tableView->setModel(pModel);ui.tableView->setItemDelegateForColumn(0, &spinBoxDelegate);/*ui.tableView->setItemDelegateForColumn(1, &comboBoxDelegate);ui.tableView->setItemDelegateForColumn(2, &pushButtonDelegate);*/
}QVariantCustom::~QVariantCustom()
{}
这篇关于QStyledItemDelegate自定义代理组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!