当前位置:网站首页>Qt自定义委托
Qt自定义委托
2022-06-10 21:20:00 【风烟倦】
Qt中的委托通常都是继承自QStyledItemDelegate或者QItemDelegate,二者的区别主要在于绘制方式,QStyledItemDelegate会使用当前样式绘制,并且能够使用qss,因此在在自定义委托时,一般使用 QStyledItemDelegate作为基类。除此之外,二者基本没有区别,写法和用法都一样。
继承 QStyledItemDelegate需要实现以下几个函数:
- createEditor():returns the widget used to change data from the model and can be reimplemented to customize editing behavior.
- setEditorData(): provides the widget with data to manipulate.
- updateEditorGeometry():ensures that the editor is displayed correctly with respect to the item view.
- setModelData():returns updated data to the model.
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
virtual void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
virtual void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;假设一个QTableView中,某一列的数据全是“选项一,选项二,选项三”中的任意一个选项,这种情况下就可以把这一列的代理设置成以QComboBox为基础的,每次修改数据时双击表格中的cell,出现下拉框来选择数据。详细代码如下:
nari_combodelegate.h
#ifndef NARI_COMBODELEGATE_H
#define NARI_COMBODELEGATE_H
class NARI_ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:
NARI_ComboDelegate(const QStringList &items, QObject *parent = NULL);
~NARI_ComboDelegate();
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;
QStringList getItemStr();
private:
QStringList m_ls_itemtext;
};
#endif // NARI_COMBODELEGATE_H
nari_combodelegate.cpp
#include "nari_combodelegate.h"
NARI_ComboDelegate::NARI_ComboDelegate(const QStringList &items, QObject *parent) :
QItemDelegate(parent)
{
m_ls_itemtext = items;
}
NARI_ComboDelegate::~NARI_ComboDelegate(){ }
QWidget *NARI_ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *editor = new QComboBox(parent);
editor->addItems(m_ls_itemtext);
editor->setEditable(false);
return editor;
}
void NARI_ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
int icurIndex = comboBox->findText(value);
comboBox->setCurrentIndex(icurIndex);
}
void NARI_ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
model->setData(index, dynamic_cast<QComboBox*>(editor)->currentText(), Qt::EditRole);
}
void NARI_ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
QStringList NARI_ComboDelegate::getItemStr()
{
return m_ls_itemtext;
}
mainwindow.cpp
// ...
ui->tableView->setItemDelegateForColumn(0, new NARI_ComboDelegate(QStringList() << "Item1" << "Item2" << "Item3"));
// ...其他委托示例
限制输入大小
nari_doubledelegate.h
#ifndef NARI_DOUBLEDELEGATE_H
#define NARI_DOUBLEDELEGATE_H
class NARI_DoubleDelegate : public QItemDelegate
{
Q_OBJECT
public :
NARI_DoubleDelegate(double minval, double maxval, QObject *parent = NULL);
~NARI_DoubleDelegate();
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:
double m_minval, m_maxval;
};
#endif // NARI_DOUBLEDELEGATE_H
nari_doubledelegate.cpp
#include "nari_doubledelegate.h"
NARI_DoubleDelegate::NARI_DoubleDelegate(double minval, double maxval, QObject *parent)
: QItemDelegate(parent)
{
m_minval = minval;
m_maxval = maxval;
}
NARI_DoubleDelegate::~NARI_DoubleDelegate(){ }
QWidget *NARI_DoubleDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setToolTip(QString::number(m_minval) + " - " + QString::number(m_maxval));
editor->setMinimum(m_minval);
editor->setMaximum(m_maxval);
return editor;
}
void NARI_DoubleDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
double value = index.model()->data(index, Qt::EditRole).toDouble();
QDoubleSpinBox *spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
spinBox->setValue(value);
}
void NARI_DoubleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDoubleSpinBox *spinBox = dynamic_cast<QDoubleSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
void NARI_DoubleDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
限制输入IP地址
nari_Ipdelegate.h
#ifndef NARI_IPDELEGATE_H
#define NARI_IPDELEGATE_H
class NARI_IpDelegate : public QItemDelegate
{
public:
NARI_IpDelegate(QObject *parent = NULL);
~NARI_IpDelegate();
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;
};
#endif // NARI_IPDELEGATE_H
nari_Ipdelegate.cpp
#include "nari_ipdelegate.h"
NARI_IpDelegate::NARI_IpDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
NARI_IpDelegate::~NARI_IpDelegate(){ }
QWidget *NARI_IpDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QLineEdit *editor = new QLineEdit(parent);
editor->setValidator(new QRegExpValidator(QRegExp("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$"), parent));
editor->setInputMask("000.000.000.000;0"); //必须加;0否则前面的正则表达式失效,;0”表示删除时默认填充为0
return editor;
}
void NARI_IpDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::EditRole).toString();
QLineEdit *le = dynamic_cast<QLineEdit*>(editor);
le->setText(value);
}
void NARI_IpDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
model->setData(index, dynamic_cast<QLineEdit*>(editor)->text(), Qt::EditRole);
}
void NARI_IpDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
边栏推荐
- Solution to "XXX has broken path" error in idea
- Array intersection of two arrays II
- Variables (automatic variables, static variables, register variables, external variables) and memory allocation of C malloc/free, calloc/realloc
- Abbexa cell free DNA kit instructions
- Array move 0
- 【phpstorm】 No data sources are configured to run this SQL and provide advanced c
- Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
- Apache相关的几个安全漏洞修复
- 【小程序】Vant滑动单元格添加点击其他位置自动关闭的功能
- Sealem Finance打造Web3去中心化金融平台基础设施
猜你喜欢
To do desktop plug-in, a good helper for office workers

Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
![[debug] could not find ref wiht POC XXX](/img/01/5fdbd3e9902458be754280464bd923.png)
[debug] could not find ref wiht POC XXX

Abbexa 1,3-dipalmitonin CLIA kit solution

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas

Principle of gravure overprint and factors affecting overprint

Sealem finance builds Web3 decentralized financial platform infrastructure
![[MySQL] summary of common data types](/img/96/010c21f0aa7b443c130c5f55e5277a.png)
[MySQL] summary of common data types

JS anchor positioning can extend many functions

What about the popular state management library mobx?
随机推荐
Abbexa cdan1 siRNA instruction manual
记录(二)
[MySQL] summary of common data types
进阶高级程序员必知必会. 要不然,蠢材吗
【MySQL】錶數據的增删查改(DML)
JVM运行时数据区
报错解决Error parsing Mapper XML
修改SpriteMask 的 frontSortingLayer 变量
C language - quick sorting in sorting
What about the popular state management library mobx?
Error parsing mapper XML
C程序实例1--个人通讯录管理系统
【C#】overide可重写从更高父级继承来的virtual方法
Only three steps are needed to learn how to use low code thingjs to connect with Sen data Dix data
(十一)TableView
Visio 转为高质量PDF
SQL exercise 4: string processing function
【MySQL】常见数据类型总结
[MySQL] Table constraints
Can I make up the exam if I fail the soft exam? Here comes the answer