更改 QT Creator 中按钮的颜色

发布于 2024-10-06 22:06:57 字数 275 浏览 4 评论 0原文

如何更改按钮颜色? 我找到了通过

button->setStyleSheet("* { background-color: rgb(255,125,100) }");

在 Ui_Window.h 中

编写来做到这一点的方法,但每次我 qmake 时,Ui_Window.h 都会被重新制作,并且我会失去颜色。

有谁知道如何永久保留按钮的颜色?我正在与 QT Creator 合作。如果有人可以指导我=D

非常感谢!

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

But everytime I qmake, the Ui_Window.h gets remade and I lose my colors.

Does anyone know how to permanently keep the colors of the button? I am working with QT Creator. If someone could direct me =D

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2024-10-13 22:06:57
  • 最简单的方法是在按钮上使用样式表:

    backgroundColourButton->setStyleSheet("背景颜色:红色");
    
  • Easiest way would be to use a style sheet on the button:

    backgroundColourButton->setStyleSheet("background-color: red");
    
君勿笑 2024-10-13 22:06:57

Aero,请注意您不得修改 Ui_Window.h 文件,因为它是由 Qt Designer 生成的。因此,每次重新编译 .ui 文件时,该头文件都会被覆盖。正如我所看到的,您正在使用 Qt Designer 将按钮添加到布局中。在这种情况下,您可以右键单击 Qt Designer 中的小部件(或在主对话框中),然后单击“更改布局...”。在那里,您可以执行以下操作:

QPushButton {
    background-color: rgb(255,125,100);
}

对于所有按钮,或对于特定按钮:

#nameOfTheButton {
    background-color: rgb(255,125,100);
}

告诉我这是否适合您。干杯,

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:

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,

清欢 2024-10-13 22:06:57

如果您仅在一个类中使用表单,则可以在调用其中的 ui->setupUi() 之后在其中添加表达式。
或者甚至直接将样式表添加到表单中,在 Qt 设计器的属性视图中查找属性“StyleSheet”。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文