How to correctly set the color of a QPushButton?

ぐ巨炮叔叔 提交于 2019-12-11 07:32:26

问题


I can successfully change the color of a QPushButton using setStyleSheet, but because I'm using QT Creator to make the GUI, every time I run qmake and make, the calls to setStyleSheet disappear.

Changing the palette of the button doesn't change its color either.

What's the best way to change to color of the button without having to manually change my ui_window.h file every time I qmake?


回答1:


Using style sheets is the right way to do, no matter you're using Qt Creator or not.

From what you are describing, it seems you are writing yourself some code into the ui_window.h, which is the wrong way to set the stylesheet.

You can set it in the constructor of your window class, or set it from the GUI editor (Qt Designer) :

  • double-click on your window.ui in the project tree displayed by Qt Creator.
  • select your QPushButton in the GUI editor.
  • locate the styleSheet property in the properties editor.
  • click on the three dots (...) : this will bring a stylesheet editor.

If you set the stylesheet with the stylesheet editor, nothing will disappear each time you are rebuilding your app.



来源:https://stackoverflow.com/questions/4395231/how-to-correctly-set-the-color-of-a-qpushbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!