当前位置:网站首页>Qt的QItemDelegate使用
Qt的QItemDelegate使用
2022-08-04 14:11:00 【物随心转】
一、介绍
Qt使用model/view 模式,一般来讲, model是数据,view把数据展示给用户,数据与界面的交互则通过delegagte来执行, 自定义的delegagte都继承于QItemDelegate类。
QItemDelegate类为模型中的数据项提供了显示和编辑的工具。QItemDelegate可以用来为基于QAbstractItemView子类的项目视图,提供自定义的显示特性和编辑器小控件。
二、使用
该类提供了在view中绘制model数据的默认实现。在QAbstractItemDelegate(该类的基类)中,已经定义了paint()和sizeHint()这两个虚函数,为了确保delegate实现自定义的view,我们可以在子类中重新实现这些虚函数,来实现自定义的外观。
用户实现代理类时,可以从两个类中继承
第一种:从QAbstractItemDelegate类中继承,至少需要实现两个函数,paint()和sizeHint()
void MyDelegate::paint(QPainter*painter,const QStyleOptionViewItem&option, constQModelIndex&index)const
{
if (index.column() == 需代理的列) {
int content = index.model()->data(index, Qt::DisplayRole).toInt(); // 取到模型中原来的内容
// ... 修改取到的值,代码略 QStyleOptionViewItem myOption = option;
myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; // 处理文本的对齐方式 (不重要)
drawDisplay(painter, myOption, myOption.rect, 修改后的值); // 写回处理后的值
drawFocus(painter, myOption, myOption.rect); // 如果当前项具有焦点,它就绘制一个焦点矩形(不重要)
} else{
QItemDelegate::paint(painter, option, index);
}
}
QSize MyDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (条件) {
return Qsize(int width, int height); // 用户定义
} else {
return QStyledItemDelegate::sizeHint(option, index);
}
}
第二种:从QItemDelegate类继承,一般使用这种方式,减少代码量
#ifndef DATEDELEGATE_H
#define DATEDELEGATE_H
#include <QtGui>
#include<QItemDelegate>
#include<QDateTimeEdit>
#include<QDebug>
#include<QModelIndex>
class dateDelegate : public QItemDelegate
{
Q_OBJECT
public:
dateDelegate(QObject *parent = 0);
// //创建委托控件
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 // DATEDELEGATE_H
三、实例
自定义表格控件,目录如下
main.cpp
#include <QApplication>
#include<QStandardItemModel>
#include<QTableView>
#include<datedelegate.h>
#include<comboboxdelegate.h>
#include<spinboxdelegate.h>
#include<QFile>
#include<QTextStream>
#include<QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//The QStandardItemModel class provides a generic model for storing custom data
QStandardItemModel model(4, 4);
QTableView tableView;
tableView.setModel(&model);
dateDelegate datedelegate;
ComboboxDelegate combbDelegate;
SpinBoxDelegate spinDelegate;
tableView.setItemDelegateForColumn(1, &datedelegate);
tableView.setItemDelegateForColumn(2, &combbDelegate);
tableView.setItemDelegateForColumn(3, &spinDelegate);
//View的setItemDelegateForColumn()函数为指定的列应用指定的Delegate
model.setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
model.setHeaderData(1, Qt::Horizontal, QObject::tr("Birthday"));
model.setHeaderData(2, Qt::Horizontal, QObject::tr("job"));
model.setHeaderData(3, Qt::Horizontal, QObject::tr("Income"));
//为表格的表头显示进行设置
QFile file(":/txt/data.txt");
if (file.open(QFile::ReadOnly))
{
QTextStream stream(&file);
QString line;
int row = 0;
do {
line = stream.readLine();
if (!line.isEmpty())
{
QStringList pieces = line.split(",", QString::SkipEmptyParts);
model.setData(model.index(row, 0, QModelIndex()), pieces.value(0));
model.setData(model.index(row, 1, QModelIndex()), pieces.value(1));
model.setData(model.index(row, 2, QModelIndex()), pieces.value(2));
model.setData(model.index(row, 3, QModelIndex()), pieces.value(3));
row++;
}
} while (!line.isEmpty());
file.close();
}
tableView.setWindowTitle(QObject::tr("Delegate"));
tableView.show();
return a.exec();
}
spinboxdelegate.h
#ifndef SPINBOXDELEGATE_H
#define SPINBOXDELEGATE_H
#include <QItemDelegate>
#include<QSpinBox>
class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit SpinBoxDelegate(QObject *parent = 0);
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 // SPINBOXDELEGATE_H
spinboxdelegate.cpp
#include "spinboxdelegate.h"
SpinBoxDelegate::SpinBoxDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const{
QSpinBox * spinbox = new QSpinBox(parent);
spinbox->setRange(1000,10000);
spinbox->installEventFilter(const_cast<SpinBoxDelegate*>(this));
return spinbox;
}
//将model中的数据赋值到控件上去
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const{
int value = index.model()->data(index).toInt();
QSpinBox * spin = static_cast<QSpinBox *>(editor);
spin->setValue(value);
}
//更新model中的数据
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const{
QSpinBox * spin = static_cast<QSpinBox *>(editor);
int value = spin->value();
model->setData(index,value);
}
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const{
editor->setGeometry(option.rect);
}
运行
代码:
https://github.com/Jelly-123/QT/tree/master/Delegate
参考:
边栏推荐
猜你喜欢
Redis 复习计划 - Redis主从数据一致性和哨兵机制
节省50%成本!京东云重磅发布新一代混合CDN产品
idea去掉spark的日志
State security organs conduct criminal arrest and summons review on Yang Zhiyuan, a suspect suspected of endangering national security
Analysis and application of portrait segmentation technology
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
leetcode 48. Rotate Image (Medium)
Is the code more messy?That's because you don't use Chain of Responsibility!
Rust 从入门到精通04-变量
自监督学习未来是掩码自编码器?KAIST最新《自监督学习掩码自编码器》研究进展
随机推荐
爬虫——动作链、xpath、打码平台使用
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置
This article sorts out the development of the main models of NLP
文字编码 - XML 教程
从理论到实践:MySQL性能优化和高可用架构,一次讲清
浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况
错误 AttributeError type object 'Callable' has no attribute '_abc_registry' 解决方案
(记录)异步并发,多线程处理表的统计
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
LM2596有没有可以替代的?LM2576可以
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
【LeetCode】1403. 非递增顺序的最小子序列
如何才能有效、高效阅读?猿辅导建议“因材因时施教”
集合划分差最小问题(01背包)
AutoCAD DWG,DXF文件导出高清图片、PDF
第四讲 SVN
Niuke.com Brush Question Record || Linked List
物联网应用发展趋势