简体   繁体   English

单击QPushButton后如何更改QPainterPath颜色

[英]How to change QPainterPath color after clicking on QPushButton

I subclassed a QPushButton so that I was able to re-implement the paintEvent(QPaintEvent *paint) method also advised by the official documentation.我对QPushButton进行了子类化,以便能够重新实现官方文档也建议的paintEvent(QPaintEvent *paint)方法。

Below is the sequence of operations:以下是操作顺序:

a) After I launch the application the button is below: a)启动应用程序后,按钮如下:

b

b) This is after I hover on it: b) 这是我在 hover 之后就可以了:

C

c) then I click the button: c)然后我点击按钮:

b

d) and finally I release the mouse d) 最后我松开鼠标

d

e) go away from the button e) go 远离按钮

b

However the problem is that the QPainterPath with which I designed the green box it should be red after I release the button .但是问题是我设计绿色框的QPainterPath在我释放按钮后应该是红色的 And, of course, it should become green again after I click again the button.当然,在我再次单击该按钮后,它应该再次变为绿色。

Below the code:代码下方:

custombutton.h自定义按钮.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自定义按钮.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主窗口.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主窗口.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();
    }
}

As you see in the MainWindow I created a function onClickedButton() which I thought would have triggered the QPainterPath into red after clicked.正如您在MainWindow中看到的,我创建了一个 function onClickedButton() ,我认为单击后会触发QPainterPath变为红色。 And in order to do that I casted it into an object (qobject_cast).为了做到这一点,我将其转换为 object (qobject_cast)。 But unfortunately it didn't work as expexted.但不幸的是,它并没有像expexted那样工作。

The solution is to create a flag that indicates the color and call update() for repainting:解决方案是创建一个指示颜色的标志并调用 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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM