Qt TableView之自定义按键进阶
主要功能:
1、开放自定义按键数量及名称
2、能够判断出哪一个按键按下
3、如有不足之处欢迎指出,后续会持续优化,欢迎关注
头文件
#ifndef BUTTONDELEGATE_H
#define BUTTONDELEGATE_H
#include <QStyledItemDelegate>
#include <QObject>
class ButtonDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
explicit ButtonDelegate(QStringList buttonNames = {"我是按键"},QObject *parent = nullptr);
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;
public:
void emitClicked()const;
mutable int abc;
signals:
void clicked(QString buttonName,const QModelIndex &index) const;
private:
int mButtonState;
int mButtonNum;
int mButtonSpace;
QPoint mMousePoint;
QStringList mButtonNames;
};
#endif // BUTTONDELEGATE_H
源文件
#include "buttondelegate.h"
#include "qapplication.h"
#include <QPushButton>
#include <QMouseEvent>
ButtonDelegate::ButtonDelegate(QStringList buttonNames , QObject *parent)
: QStyledItemDelegate{parent}
{
mButtonNames = buttonNames;
mButtonNum = buttonNames.count();
mButtonSpace = 1;
}
//paint函数是const类型的函数,不能改变成员变量的值,但能改变mutable修饰的成员变量的值
void ButtonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index)
QStyleOptionViewItem optionItem(option);
// initStyleOption(&optionItem,index);
// if(option.state.testFlag(QStyle::State_HasFocus))
// viewOption.state = viewOption.state^QStyle::State_HasFocus;
// QStyledItemDelegate::paint(painter,viewOption,index);
// initStyleOption(&optionItem,index);
//根据按钮数量,确定按键长宽及坐标
int buttonWidth = (optionItem.rect.width()-(mButtonNum+1)*mButtonSpace)/mButtonNum;
int buttonHeight = optionItem.rect.height() - 2*mButtonSpace;
int buttonLeft = optionItem.rect.left() + mButtonSpace;
int buttonTop = optionItem.rect.top() + mButtonSpace;
for(int i = 0; i < mButtonNum; i++){
QStyleOptionButton styleButton; //绘制按键
styleButton.rect = QRect(buttonLeft,buttonTop,buttonWidth,buttonHeight);
buttonLeft += buttonWidth + mButtonSpace;
styleButton.text = mButtonNames.at(i);
styleButton.state = QStyle::State_Enabled; //设置按键状态
if((mButtonState == 0)&&(styleButton.rect.contains(mMousePoint))){
styleButton.state |= QStyle::State_MouseOver;
qDebug()<<" QStyle::State_MouseOver";
}else if((mButtonState == 1) && (styleButton.rect.contains(mMousePoint))){
styleButton.state |= QStyle::State_Raised;
qDebug()<<"QStyle::State_Raised";
}
QApplication::style()->drawControl(QStyle::CE_PushButton,&styleButton,painter);
}
}
bool ButtonDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
Q_UNUSED(model)
bool ret = false;
mButtonState = -1;
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
mMousePoint = mouseEvent->pos();
qDebug()<<"editorEvent: "<<mouseEvent->type();
int buttonWidth = (option.rect.width()-(mButtonNum+1)*mButtonSpace)/mButtonNum;
int buttonHeight = option.rect.height() - 2*mButtonSpace;
int buttonLeft = option.rect.left()+mButtonSpace;
int buttonTop = option.rect.top() + mButtonSpace;
for(int i = 0; i < mButtonNum; i++){
QStyleOptionButton styleButton;
styleButton.rect = QRect(buttonLeft,buttonTop,buttonWidth,buttonHeight);
buttonLeft += buttonWidth + mButtonSpace;
if(!styleButton.rect.contains(mouseEvent->pos())){
continue;
}else{
switch (mouseEvent->type()) {
case QEvent::MouseMove:
ret = false;
break;
case QEvent::MouseButtonPress:
mButtonState = 0;
ret = true;
break;
case QEvent::MouseButtonRelease:
mButtonState = 1;
ret = true;
emit clicked(mButtonNames.at(i),index);
break;
case QEvent::MouseButtonDblClick:
ret = true;
break;
default:
ret = false;
break;
}
}
}
return ret;
}