单击 QPushButton 后如何更改 QPainterPath 颜色

How to change QPainterPath color after clicking on QPushButton

我对 QPushButton 进行了子类化,这样我就能够重新实现官方文档也建议的 paintEvent(QPaintEvent *paint) 方法。

以下是操作顺序:

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

b) 这是我将鼠标悬停在上面之后的样子:

c) 然后我点击按钮:

d) 最后松开鼠标

e) 离开按钮

但是问题是我设计绿色框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();
}

mainwindow.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

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

mainwindow.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, 15, 80, 5), 2, 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));
}