简体   繁体   English

如何以编程方式更改 Qt 中按钮的样式表?

[英]How to programmatically change style sheet of buttons in Qt?

I have so many buttons on a dialog and I want to change style sheets of them under some conditions.我在对话框上有很多按钮,我想在某些情况下更改它们的样式表。 Button object names are like below:按钮对象名称如下:
btn_1 btn_1
btn_2 btn_2
btn_3 btn_3
.. ..
btn_20 btn_20


When I clicked one of these numerical buttons and later to another simple button, I want to change first clicked numerical button style sheet.当我单击其中一个数字按钮然后单击另一个简单按钮时,我想更改第一个单击的数字按钮样式表。 How can I access that selected numerical button?如何访问该选定的数字按钮?

Edit: What I mean by picture编辑:我的意思是图片
纽扣


I am trying to set colors of left column buttons (has numerically ordered object names) with right column buttons.我正在尝试使用右列按钮设置左列按钮的颜色(具有按数字排序的对象名称)。 User will be clicked numerical buttons first and then color named buttons.用户将首先点击数字按钮,然后点击颜色命名按钮。

When you click on the first button, get its name using the method objectName() , then when you need to change the style, just specify in the method当您点击第一个按钮时,使用方法objectName()获取其名称,然后当您需要更改样式时,只需在方法中指定

setStyleSheet(QString(QPushButton#) + button->objectName() + QString("{... }");

I can write the example-program, but I do not fully understand what you want我可以写示例程序,但我不完全明白你想要什么

You have to use the setStyleSheet method but you have to keep the reference of the button pressed, and that can be done using the sender method that returns the object that emitted the signal.您必须使用 setStyleSheet 方法,但必须保持按下按钮的引用,这可以使用返回发出信号的对象的 sender 方法来完成。

#include <QtWidgets>

class MainWindow: public QMainWindow{
    Q_OBJECT
public:
    MainWindow(QWidget *parent=nullptr):
        QMainWindow(parent),
        current_button(nullptr)
    {
        QWidget *widget = new QWidget;
        setCentralWidget(widget);
        QHBoxLayout *hlay = new QHBoxLayout(widget);
        QVBoxLayout *number_lay = new QVBoxLayout;
        QVBoxLayout *color_lay = new QVBoxLayout;
        hlay->addLayout(number_lay);
        hlay->addLayout(color_lay);

        for(int i=0; i<20; i++){
            QPushButton *button = new QPushButton(QString("btn_%1").arg(i+1));
            connect(button, &QPushButton::clicked, this, &MainWindow::number_clicked);
            number_lay->addWidget(button);
        }
        color_lay->addStretch();
        for(const QString & colorname: {"Red", "Green", "Blue"}){
            QPushButton *button = new QPushButton(colorname);
            connect(button, &QPushButton::clicked, this, &MainWindow::color_clicked);
            color_lay->addWidget(button);
            button->setProperty("color", colorname.toLower());
            button->setStyleSheet(QString("background-color: %1").arg(colorname));
        }
        color_lay->addStretch();
    }
private slots:
    void number_clicked(){
        current_button = qobject_cast<QPushButton *>(sender());
    }
    void color_clicked(){
        if(current_button){
            QString colorname = sender()->property("color").toString();
            current_button->setStyleSheet(QString("background-color: %1").arg(colorname));
        }
    }
private:
    QPushButton *current_button;
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
#include "main.moc"

在此处输入图像描述

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

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