简介
通过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); }"));