一、為單個控制元件添加樣式



QLabel{
color:black;
font: 75 9pt "微軟雅黑";
border-radius: 5px;
radius:2px;
background:qlineargradient(x1:0, y1:0, x2:1, y2:0,stop:0 rgb(112, 144, 101),stop:1 rgb(107, 255, 119));
}
二、為整個軟體的同類控制元件添加樣式
2.1 添加.qrc資源檔案


命名一個新的空白的.qrc檔案



可以再創建幾個資源過濾器,方便我們分類不同的資源型別,比如qss用來存放樣式表,pic用來存放圖片等等,
同樣命名一個空白的.qss檔案



2.2 撰寫qrc和qss檔案


QLabel
{
color:black;
font: 75 9pt "微軟雅黑";
border-radius: 5px;
radius:2px;
background:qlineargradient(x1:0, y1:0, x2:1, y2:0,stop:0 rgb(112, 144, 101),stop:1 rgb(107, 255, 119));
}
QPushButton,QLineEdit,QComboBox{
background-color: azure;
color:deepskyblue;
}
QPushButton:pressed{
color:red;
}
2.3 轉換資源檔案


之所以要添加_rc,是因為Qt Designer匯入資源檔案時默認是加_rc的,這里為了 與Qt Designer保持一致,
2.4 匯入.py資源檔案并設定style
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
try:
app = QApplication(sys.argv) # 實體化一個應用物件,sys.argv是一組命令列引數的串列,Python可以在shell里運行,這是一種通過引數來選擇啟動腳本的方式,
myshow = MyUi()
file = QFile(":/qss/style")
file.open(QFile.ReadOnly)
fileText = QTextStream(file)
styleSheet = fileText.readAll()
myshow.setStyleSheet(styleSheet)
myshow.show()
sys.exit(app.exec_()) # 確保主回圈安全退出
except Exception as ex:
print(ex)


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/305456.html
標籤:python
