Qt之modelview(自定义委托代理)

Qt之modelview(⾃定义委托代理)
简介
通过model和tableview配合使⽤能够很好的管理数据,就能够实现⽐较复杂的功能,例如复选框,进度条,⾃定义按钮等。⽹上关于这部分的教程⽐较多,但⼤多都不详细,因此结合⽹上的资料给出⼀个⽐较详细的教程。
效果
部分源代码
计量秤头⽂件.h
#ifndef BTNDELEGATE_H
#define BTNDELEGATE_H
#include <QStyledItemDelegate>
#include <QObject>
class QTableView;
class QPushButton;
class QPersistentModelIndex;
class BtnDelegate:public QStyledItemDelegate
{
Q_OBJECT
public:
enum BtnStatus
{
None_Status =-1,
Hover_Status,
Pressed_Status
};
public:
BtnDelegate(QObject *parent =nullptr);
金瓶梅词话本
~BtnDelegate() override;
安乐死论文QWidget *createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index)const override;
// painting绘制按钮
void paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index)const override;
bool editorEvent(QEvent *event, QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index) override; bool helpEvent(QHelpEvent *event,QAbstractItemView *view,const QStyleOptionViewItem &option,const QModelIndex &index) override; void setBtns(const QList<QPushButton *>&value);
void setBtnWidth(int value);
void setBtnHeight(int value);
void setBtnHSpace(int value);
Q_SIGNALS:
void Signal0(int);
void Signal1(int);
void Signal2(int);
void Signal3(int);
private:
BtnStatus currentStatus = None_Status;
QPoint mousePoint;
int btnWidth;
int btnHeight;
int btnHSpace;
QList<QPushButton*> btns;
};
#endif// BTNDELEGATE_H
源⽂件.cpp
BtnDelegate::BtnDelegate(QObject *parent):QStyledItemDelegate(parent)
仔仔动力联盟{
}
BtnDelegate::~BtnDelegate()
{
foreach(auto var,btns)
{
if(var)
{
delete var;
var =nullptr;
}
}
btns.clear();
}
QWidget *BtnDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &option,const QModelIndex &index)const {
Q_UNUSED(parent)
Q_UNUSED(option)
矩阵干扰Q_UNUSED(index)
return nullptr;
}
void BtnDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index)const
{
QStyledItemDelegate::paint(painter,option,index);
int count = unt();
int hMarginHalf =(width()- btnWidth*count - btnHSpace*(count-1))/2;
int vMarginHalf =(height()-btnHeight)/2;
for(int i=0;i&unt();i++)
{
int x = left()+ i * btnWidth + hMarginHalf + btnHSpace * i;
int y = p()+ vMarginHalf;
QStyleOptionButton styleOptionBtn;
< =QRect(x,y,btnWidth,btnHeight);
styleOptionBtn.state |= QStyle::State_Enabled;
ontains(mousePoint))
{
if(currentStatus == BtnStatus::Hover_Status)
styleOptionBtn.state = QStyle::State_MouseOver;
if(currentStatus == BtnStatus::Pressed_Status)
styleOptionBtn.state = QStyle::State_Sunken;
}
乌塔教学设计
QWidget* widget = btns.at(i);
widget->style()->drawControl(QStyle::CE_PushButton,&styleOptionBtn,painter,widget);
}
}
void BtnDelegate::setBtns(const QList<QPushButton *>&value)
{
btns = value;
}
void BtnDelegate::setBtnWidth(int value)
{
btnWidth = value;
}
void BtnDelegate::setBtnHeight(int value)
{
btnHeight = value;
}
void BtnDelegate::setBtnHSpace(int value)
{
btnHSpace = value;
}
```cpp
QPushButton* btn =new QPushButton;
btn->setToolTip(QStringLiteral("初始参数"));
btn->setStyleSheet(QStringLiteral(
"QPushButton { border-image: url(:/imgs/imgs/res_rcd/edit01.png); } \
QPushButton::hover { border-image: url(:/imgs/imgs/res_rcd/edit02.png); } \
QPushButton::pressed { border-image: url(:/imgs/imgs/res_rcd/edit02.png); } \
QPushButton::disabled { border-image: url(:/imgs/imgs/res_rcd/edit02.png); }"));

本文发布于:2024-09-21 04:26:18,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/453302.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:能够   资料   定义   进度条   数据   按钮   复选框
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议