簡體   English   中英

Qt:改變字體粗細

[英]Qt: change font weight

我希望QLabel文本介於粗體和普通樣式之間,我相信設置font-weight應該是我問題的答案。

在Qt文檔中,我發現有兩種選擇如何更改font-weight:

  1. 從cpp側經過: QFont::setWeight()方法接受數字0-99

    http://doc.qt.io/qt-4.8/qfont.html#Weight-enum

  2. 從Qss樣式到: font-weight屬性,它接受數字100,200,...,900

    http://doc.qt.io/qt-4.8/stylesheet-reference.html#font-weight

我嘗試了兩種方法,但似乎沒有任何效果。 我總是得到正常或普通的大膽風格,而且介於兩者之間。

例:

QLabel* test1 = new QLabel("Font-weight testing");
test1->show();

QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(40);
test2->setFont(font);
test2->show();

QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("font-weight: 400");
test3->show();

在上面的示例中,我創建了3個標簽。 一個沒有任何額外設置,一個是我通過setWeight方法改變了字體粗細,另一個是通過Qss樣式改變字體粗細。 但這三個人最終會完全一樣。

我甚至試圖使字體更大,啟用抗鋸齒,或使用不同的字體,但沒有任何幫助。

QFont::setWeight方法期望其輸入值是QFont::Weight枚舉值之一。

http://doc.qt.io/qt-5/qfont.html#setWeight

正確的版本:

QLabel* test2 = new QLabel("Font-weight testing");
QFont font = test2->font();
font.setWeight(QFont::Bold);
test2->setFont(font);

此外,您在QSS版本中有兩個錯誤。 首先,您沒有為規則指定選擇器。 其次,400的值對應於'普通'字體。

https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight

正確的版本:

QLabel* test3 = new QLabel("Font-weight testing");
test3->setStyleSheet("QLabel { font-weight: bold; }");

像這樣使用函數setWeightsetWeight(QFont::ExtraBold);

QFont font;
font.setWeight(QFont::ExtraBold); // set font weight with enum QFont::Weight
font.setPixelSize(25); // this for setting font size
ui->label->setFont(font);

void QFont :: setWeight(int weight):將字體的權重設置為weight,該值應為QFont :: Weight枚舉中的值。

圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM