改变QT中按钮的颜色

Changing color of a pushButton in QT

我想在单击按钮后更改 QT 中按钮的颜色。问题是,当我有多个按钮时,所有 pushbButton 的颜色都会改变。我怎样才能只制作我点击更改的那个? PS: 我是初学者所以如果你能详细解释你的解决方案就太好了,sry:D.

我的代码:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

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

int i=1;

void MainWindow::on_pushButton_clicked()
{
    if((i==1))
    {
        setStyleSheet("QPushButton { background-color: grey; }\n"
                      "QPushButton:enabled { background-color: rgb(200,0,0); }\n");
        i=i+1;

    }
    else
    {
        if((i==2))
        {
            setStyleSheet("QPushButton { background-color: grey; }\n"
                          "QPushButton:enabled { background-color: rgb(0,200,0); }\n");
         i=i+1;
        }
        else
        {
            setStyleSheet("QPushButton { background-color: grey; }\n"
                          "QPushButton:enabled { background-color: rgb(0,0,200); }\n");
            i=i-2;
        }
    }

}

简短回答:您的 QSS 不符合您的目的,QPushButton {}QPushButton [=37] 的所有样式=](它的祖先),这就是当前结果的原因。使用 #yourButtonName {} 即可。

代码及说明:

可以通过 sender() 在插槽中获取按钮的名称,但是像现在这样使用它会有点难看,看看你自己:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->myPushButton, &QPushButton::clicked, this, &MainWindow::onClicked);
}

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

void MainWindow::onClicked()
{
    QPushButton* target = qobject_cast<QPushButton*>(sender());
    if (target != nullptr)
    {
        target->setStyleSheet(QString("#%1 { background-color: red; }").arg(target->objectName()));
    }
}

注意:使用 connect 而不是设计者生成的插槽并使用 sender(),以便您可以将此插槽重复用于其他按钮。

好吧,有时您希望编写较少的样式生成代码,因为它很快变得不可读,然后 dynamic properties 就派上用场了:

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStyle>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->myPushButton, &QPushButton::clicked, this, &MainWindow::onClicked);
}

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

void MainWindow::onClicked()
{
    QPushButton* target = qobject_cast<QPushButton*>(sender());
    target->setProperty("clicked", true);
    target->style()->unpolish(target);
    target->style()->polish(target);
}

通过这种方式,您可以让小部件“记住”属性 中的某个状态,恕我直言,这是一种更简洁的方式。此外,带有 QStyle 的小 hack 成为强制性的。单击按钮的样式本身可以添加到 MainWindow 的样式表中一次(我已经在 Qt Designer 中完成,您也可以在构造函数中完成):

QPushButton[clicked=true]
{
    background-color: green;
}

How can I make only the one i clicked on Change? you set the property to the button instance then:

调用该按钮中的设置样式:ui->pushButton->

喜欢做:

void MainWindow::on_pushButton_clicked()
{
    if((i==1))
    {
        ui->pushButton->setStyleSheet("QPushButton { background-color: grey; }\n"
                      "QPushButton:enabled { background-color: rgb(200,0,0); }\n");
        i=i+1;
    }
    ....
    ...