18

This might be an easy question, but I'm trying to give a color to a specific QLabel in my application and it doesn't work.

The code I tried is the following :

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setStyleSheet("QLabel#nom_plan_label {color: yellow}")

Any hint would be appreciated

4

1 に答える 1

31

使用しているスタイルシートの構文にはいくつか問題があります。

まず、IDセレクター (つまり)はウィジェット#nom_plan_labelの を参照する必要があります。objectName

次に、スタイルシートが祖先ウィジェットに適用され、特定のスタイル ルールを特定の子孫ウィジェットにカスケードする場合にのみ、セレクターを使用する必要があります。スタイルシートを 1 つのウィジェットに直接適用する場合は、セレクター (およびブレース) を省略できます。

上記の 2 つのポイントを考えると、サンプル コードは次のいずれかになります。

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setObjectName('nom_plan_label')
nom_plan_label.setStyleSheet('QLabel#nom_plan_label {color: yellow}')

または、より簡単に:

nom_plan_label = QtGui.QLabel()
nom_plan_label.setText(nom_plan_vignette)
nom_plan_label.setStyleSheet('color: yellow')
于 2011-12-20T17:24:14.667 に答える