2016-11-17 60 views
0

在QT中對標籤進行樣式設計好像最基本的東西讓我感動。如何使用代碼

我想在QT中設置標籤的背景顏色。我知道我可以使用樣式表通過右鍵單擊並添加:background-color: blue;或其他東西。那效果很好。

但是我怎樣才能做到這一點,沒有GUI視圖。

我知道我可以在mainwindow.cpp改變主窗口背景色添加

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    this->setStyleSheet("background-color: blue }"); 
} 

,但我怎麼能與目標對象名TestLabel標籤,以及在何處放置代碼?

我試圖

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    this->setStyleSheet("QLabel:TestLabel { background-color: blue }"); 
} 

但是,弄亂我的程序。它編譯,但沒有按照預期執行。

回答

1

可以使用ID selector語法:

setStyleSheet("QLabel#TestLabel { background-color: blue }"); 

這將目標鎖定在特定QLabel,其對象名稱是TestLabel

0

您可以使用setStyleSheet()的標籤太:

ui->TestLabel->setStyleSheet("background-color: blue");