2013-08-05 167 views
2

我想攔截QSlider上的QPaintEvent並繪製它。但是我無法找到關於物體幾何形狀的細節。我可以知道整個窗口小部件的rect(),但是怎樣才能知道第一個tickmark的位置或者窗口小部件矩形中最後一個的位置? (跟蹤頻道的左側和右側有空白)。或「手柄」的矩形?自定義繪圖的QSlider(Qt)

回答

5

感謝回覆中的提示。經過一番調查後,這似乎工作。對於情況下,如果是使用的人:

QStyleOptionSlider opt; 
slider->initStyleOption(&opt); 

QStyle *styl=style(); 
Rect rectHandle=styl->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderHandle, NULL); 
Rect rectGroove=styl->subControlRect(QStyle::CC_Slider, &opt, QStyle::SC_SliderGroove, NULL); 

int avl=styl->pixelMetric(QStyle::PM_SliderSpaceAvailable, &opt, this); // width in an horizontal slider of the groove (width of widget - margins) 
1

你是否確定要重新實現繪畫事件?
也許這將足以摧毀自定義樣式表?
下面是從doks qslider一個例子:

QSlider::groove:horizontal { 
    border: 1px solid #999999; 
    height: 8px; /* the groove expands to the size of the slider by default. by giving it a height, it has a fixed size */ 
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 #c4c4c4); 
    margin: 2px 0; 
} 

QSlider::handle:horizontal { 
    background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b4b4b4, stop:1 #8f8f8f); 
    border: 1px solid #5c5c5c; 
    width: 18px; 
    margin: -2px 0; /* handle is placed by default on the contents rect of the groove. Expand outside the groove */ 
    border-radius: 3px; 
} 
+0

謝謝,其實更容易爲我攔截Q漆,另外我想dinamically畫它,當其他值的變化重繪背景(上下文是色彩定義HSL,每個滑塊繪製可用色域取決於其他值) – Joan

1

你有沒有考慮過使用setStyleSheet呢? 如果你真的想自己繪製它,你可以看看它是如何在Qt源代碼中完成的:qt/src/gui/widgets/qslider.cpp

+0

感謝提示源,從那裏和一些測試,我可以找到我需要的一切。 – Joan