2014-03-06 161 views

回答

12

Font properties如果未明確設置,則從父代繼承到子代。您可以通過setFont()方法更改QGroupBox的字體,但是您需要通過明確重置其子級的字體來中斷繼承。如果您不想爲每個孩子(例如,每個孩子上分別設置)設置此選項,則可以添加一箇中間控件,例如,像

QGroupBox *groupBox = new QGroupBox("Bold title", parent); 

// set new title font 
QFont font; 
font.setBold(true); 
groupBox->setFont(font); 

// intermediate widget to break font inheritance 
QVBoxLayout* verticalLayout = new QVBoxLayout(groupBox); 
QWidget* widget = new QWidget(groupBox); 
QFont oldFont; 
oldFont.setBold(false); 
widget->setFont(oldFont); 

// add the child components to the intermediate widget, using the original font 
QVBoxLayout* verticalLayout_2 = new QVBoxLayout(widget); 

QRadioButton *radioButton = new QRadioButton("Radio 1", widget); 
verticalLayout_2->addWidget(radioButton); 

QRadioButton *radioButton_2 = new QRadioButton("Radio 2", widget); 
verticalLayout_2->addWidget(radioButton_2); 

verticalLayout->addWidget(widget); 

還要注意,到窗口小部件分配新字體時,「從這個字體的屬性相結合,與小部件的默認字體,形成小部件的最後字體」。


一個更簡單的方法是使用樣式表 - 不像CSS,不像正常的字體和顏色繼承,properties from style sheets are not inherited

groupBox->setStyleSheet("QGroupBox { font-weight: bold; } "); 
+0

此方法是否也適用於對話標題?謝謝。 – user1899020

+0

非常有啓發性的答案。非常感謝!!!我認爲這種方式也適用於對話標題。大!!! – user1899020

+0

我剛剛嘗試過它,並沒有馬上工作,所以我不確定是否可以完成 - 至少如果它是頂級窗口/對話框,那麼本機窗口系統可能會阻止修改字體。可能這有助於:http://www.qtcentre.org/threads/25974-stylesheet-to-QDialog-Title-Bar –

0

以上回答是正確的。 這裏有幾個額外的細節可能會有所幫助:

1)我在

Set QGroupBox title font size with style sheets

瞭解到QGroupBox::title不支持字體屬性,所以你不能設置標題的字體辦法。你需要像上面那樣做。

2)我發現setStyleSheet()方法比使用QFont更「精簡」一點。也就是說,您還可以執行以下操作:

groupBox->setStyleSheet("font-weight: bold;"); 
widget->setStyleSheet("font-weight: normal;"); 
相關問題