Qt TableView之自定义代理按键进阶(一)

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;
}


  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jkdon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值