微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单击 QPushButton 后如何更改 QPainterPath 颜色

如何解决单击 QPushButton 后如何更改 QPainterPath 颜色

我对 QPushButton 进行了子类化,以便我能够重新实现官方文档所建议的 paintEvent(QPaintEvent *paint) 方法

以下是操作顺序:

a) 启动应用程序后,按钮如下:

b

b) 这是在我将鼠标悬停在其上之后:

c

c) 然后我点击按钮:

b

d) 最后我松开鼠标

d

e) 离开按钮

b

但是问题是我用来设计绿色框的QPainterPath在我松开按钮后它应该是红色的。而且,当然,在我再次单击该按钮后,它应该会再次变为绿色。

代码下方:

custombutton.h

class CustomButton : public QPushButton
{
    Q_OBJECT
public:
    CustomButton(QWidget *parent = nullptr);
    ~CustomButton();

    QString FirstName = "MACHINE";
    QString LastName = "CONTROL";

protected:
    void paintEvent(QPaintEvent *);
};

custombutton.cpp

CustomButton::CustomButton(QWidget *parent) : QPushButton(parent)
{
    setGeometry(150,150,110,110);
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet(
        "QPushButton{background-color: lightGray;border: 1px solid black; border-radius: 5px;}"
        "QPushButton:hover{background-color: gray;}"
        "QPushButton:pressed{background-color: lightGray;}");
}

CustomButton::~CustomButton()
{

}    

void CustomButton::paintEvent(QPaintEvent *paint)
{
    QPushButton::paintEvent(paint);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    path.addRoundedRect(QRectF(15,15,80,5),2,2);
    QPen pen(Qt::black,0.3);
    p.setPen(pen);
    p.fillPath(path,Qt::darkGreen);
    p.drawPath(path);
    p.save();

    p.drawText(QPoint(20,60),FirstName);
    p.drawText(QPoint(20,80),LastName);
    p.setFont(QFont("Arial",10));
    p.restore();
}

ma​​inwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

public slots:
    void onClickedButton();
private:
    Ui::MainWindow *ui;
    CustomButton *newBtn;
};

ma​​inwindow.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    newBtn = new CustomButton(this);
    newBtn->show();
    connect(newBtn,&QPushButton::clicked,this,&MainWindow::onClickedButton);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onClickedButton()
{
    QPushButton* target = qobject_cast<QPushButton*>(sender());
    if (target != nullptr)
    {
        QPainter p;
        p.setRenderHint(QPainter::Antialiasing);
        QPainterPath path;
        path.addRoundedRect(QRectF(15,2);
        QPen pen(Qt::black,0.3);
        p.setPen(pen);
        p.fillPath(path,Qt::darkRed);
        p.drawPath(path);
        p.save();
        p.restore();
    }
}

正如您在 MainWindow 中看到的,我创建了一个函数 onClickedButton(),我认为它会在点击后触发 QPainterPath 变为红色。为了做到这一点,我将它转换为一个对象 (qobject_cast)。但不幸的是它并没有像预期的那样工作。

解决方法

解决方案是创建一个指示颜色的标志并调用 update() 进行重绘:

#ifndef CUSTOMBUTTON_H
#define CUSTOMBUTTON_H

#include <QPushButton>

class CustomButton : public QPushButton
{
    Q_OBJECT
public:
    CustomButton(QWidget *parent = nullptr);
    ~CustomButton();

    QString FirstName = "MACHINE";
    QString LastName = "CONTROL";
private Q_SLOTS:
    void handleClicked();
protected:
    void paintEvent(QPaintEvent *);
private:
    bool state;
};
#endif // CUSTOMBUTTON_H
#include "custombutton.h"

#include <QPainter>
#include <QPainterPath>


CustomButton::CustomButton(QWidget *parent) : QPushButton(parent),state(true)
{
    setGeometry(150,150,110,110);
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet(
        "QPushButton{background-color: lightGray;border: 1px solid black; border-radius: 5px;}"
        "QPushButton:hover{background-color: gray;}"
        "QPushButton:pressed{background-color: lightGray;}");
    connect(this,&QPushButton::clicked,this,&CustomButton::handleClicked);
}

CustomButton::~CustomButton()
{

}

void CustomButton::handleClicked()
{
    state = !state;
    update();
}

void CustomButton::paintEvent(QPaintEvent *paint)
{
    QPushButton::paintEvent(paint);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    QPainterPath path;
    path.addRoundedRect(QRectF(15,15,80,5),2,2);
    QPen pen(Qt::black,0.3);
    p.setPen(pen);
    p.fillPath(path,state ? Qt::darkGreen: Qt::darkRed);
    p.drawPath(path);

    p.drawText(QPoint(20,60),FirstName);
    p.drawText(QPoint(20,80),LastName);
    p.setFont(QFont("Arial",10));
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。