C++ 在 QT Creator 中更改按钮的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4394289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the color of a Push Button in QT Creator
提问by Aero Chocolate
How do I change a buttons color? I have found ways to do it by writing
如何更改按钮颜色?我找到了通过写作来做到这一点的方法
button->setStyleSheet("* { background-color: rgb(255,125,100) }");
in Ui_Window.h
在 Ui_Window.h
But everytime I qmake, the Ui_Window.h gets remade and I lose my colors.
但是每次我进行 qmake 时,Ui_Window.h 都会重新制作并且我失去了颜色。
Does anyone know how to permanently keep the colors of the button? I am working with QT Creator. If someone could direct me =D
有谁知道如何永久保持按钮的颜色?我正在与 QT Creator 合作。如果有人可以指导我 =D
Thanks so much!
非常感谢!
回答by Sagar Patel
Easiest way would be to use a style sheet on the button:
backgroundColourButton->setStyleSheet("background-color: red");
最简单的方法是在按钮上使用样式表:
backgroundColourButton->setStyleSheet("background-color: red");
回答by Didac Perez Parera
Aero, take into account that you MUST not modify Ui_Window.h file since it is generated by Qt Designer. So, every time you recompile the .ui file, this header file will be overwriten. As I see, you are using Qt Designer to add buttons to the layout. In this case, you can right click on a widget (or in the main dialog) in Qt Designer and click then in 'Change layout...'. On there, you can do something like:
Aero,请注意您不得修改 Ui_Window.h 文件,因为它是由 Qt Designer 生成的。所以,每次重新编译.ui 文件时,这个头文件都会被覆盖。如我所见,您正在使用 Qt Designer 向布局添加按钮。在这种情况下,您可以在 Qt 设计器中右键单击小部件(或在主对话框中),然后单击“更改布局...”。在那里,您可以执行以下操作:
QPushButton {
background-color: rgb(255,125,100);
}
for all buttons, or for an specific button:
对于所有按钮或特定按钮:
#nameOfTheButton {
background-color: rgb(255,125,100);
}
Tell me if this works for you. Cheers,
告诉我这是否适合你。干杯,
回答by Raiv
If you use your form only in one class, you may add your expression there, after calling to ui->setupUi()
in it.
Or even add stylesheet directly to form, look for property 'StyleSheet' in properties view of Qt designer.
如果你只在一个类中使用你的表单,你可以在那里添加你的表达式,然后ui->setupUi()
在里面调用。或者甚至直接将样式表添加到表单中,在 Qt 设计器的属性视图中查找属性“StyleSheet”。