2011-01-04 9 views
9

QWidgetの "Slider"サブクラスを作成しました。Qtのスタイルシートでスタイルを変更したいと思っています。アプリケーションスタイルシートのこの設定がすべてのスライダに適用されるように、ウィジェットをQtアプリケーションに宣言する方法はありますか?カスタムQtクラスのCSSセレクタ

Slider { background-color:blue; } 

これができない場合は、このようなクラスを使用できますか?

QWidget.slider { background-color:blue; } 
+0

idを試しましたか?ie #blah {...})またはクラス(つまり.schmarr {...})セレクタ – DwB

答えて

16

ウィジェットには、metaオブジェクト経由でアクセスできる「className()」メソッドがあります。私の場合では、これは次のとおりです。

slider.metaObject()->className(); 
// ==> mimas::Slider 

「スライダー」クラスが名前空間にあるので、あなたはスタイリングのための完全修飾名を使用する必要があります(交換「::」で「 - 」):

.slider { background-color:blue; } 

C++スライダクラス:

mimas--Slider { background-color:blue; } 

別の解決策は、クラスのプロパティを定義して、先頭のドットでそれを使用することである

Q_PROPERTY(QString class READ cssClass) 
... 
QString cssClass() { return QString("slider"); } 

対象に、CSSで定義された色とスタイルでスライダーを描画するために、これはあなたがそれら(link text)を取得する方法ですが:

// background-color: 
palette.color(QPalette::Window) 

// color: 
palette.color(QPalette::WindowText) 

// border-width: 
// not possible (too bad...). To make it work, you would need to copy paste 
// some headers defined in qstylesheetstyle.cpp for QRenderRule class inside, 
// get the private headers for QStyleSheetStyle and change them so you can call 
// renderRule and then you could use the rule to get the width borders. But your 
// code won't link because the symbol for QStyleSheetStyle are local in QtGui. 
// The official and supported solution is to use property: 

// qproperty-border: 
border_width_ // or whatever stores the Q_PROPERTY border 

そして最後に、ノートにCSSのQPalette値:

color      = QPalette::WindowText 
background     = QPalette::Window 
alternate-background-color = QPalette::AlternateBase 
selection-background-color = QPalette::Highlighted 
selection-color   = QPalette::HighlightedText