問題描述
我有一個 QPushButton,上面有一個文本和圖標.我想讓按鈕上的文字變成粗體和紅色.查看其他論壇,用谷歌搜索并失去了我的希望.如果按鈕有圖標,似乎沒有辦法做到這一點(當然,如果您不創建一個文本+前圖標的新圖標).這是唯一的方法嗎?有人有更好的主意嗎?
I have a QPushButton and on that I have a text and and icon. I want to make the text on the button to be bold and red. Looked at other forums, googled and lost my hope. Seems there is no way to do that if the button has an icon (of course if you don't create a new icon which is text+former icon). Is that the only way? Anyone has a better idea?
推薦答案
你真的不需要子類來改變按鈕的格式,而是使用樣式表,例如
You really don't need to subclass to change the formatting of your button, rather use stylesheets e.g.
QPushButton {
font-size: 18pt;
font-weight: bold;
color: #ff0000;
}
將此應用于要更改的按鈕將使按鈕文本變為 18pt、粗體和紅色.您可以通過 widget->setStyleSheet()
Applying this to the button that you want to change will make the buttons text 18pt, bold and red. You can apply via widget->setStyleSheet()
將此應用于上面層次結構中的小部件將為下面的所有按鈕設置樣式,QT 樣式表 機制非常靈活且文檔齊全.
Applying this to a widget in the hierarchy above will style all the buttons underneath, the QT stylesheet mechanism is very flexible and fairly well documented.
您也可以在設計器中設置樣式表,這將立即為您正在編輯的小部件設置樣式
You can set stylesheets in the designer too, this will style the widget that you are editing immediately
這篇關于Qt - QPushButton 文本格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!