實際上,您只能使用樣式表和groove
子控件上的border-image
屬性來實現此目的。
例如,像這樣的邊界圖像:
![Slider decoration image](https://i.stack.imgur.com/815ss.png)
爲了更好地看到什麼去哪裏,B是10像素寬,空間是2,且A是12像素寬。
QSlider::groove {
border-image: url(:/slider-decoration.png) 0 12 0 10;
border-left: 10px solid transparent;
border-right: 12px solid transparent;
}
QSlider::handle {
width: 8px;
background-color: green;
}
它給你什麼,你希望:
![resulting slider](https://i.stack.imgur.com/Y6Ipe.png)
這個方案有一個小問題,但。鼠標忽略邊框的存在,並且在手柄位置和鼠標位置之間的兩端附近存在偏移。 所以,爲了避免這種情況,手柄具有一路去到任何一方,但我們增加了一個透明的邊框給它,使它看起來像它只是圖像邊界之前停止:
QSlider::groove {
border-image: url(:/slider-decoration.png) 0 12 0 10;
border-left: 10px;
border-right: 12px;
}
QSlider::handle {
width: 8px;
/* add borders to the handle that will sit above the groove border */
border-right: 10px solid transparent;
border-left: 12px solid transparent;
/* negative margins to allow the handle borders
to go past the groove borders */
margin-right: -10px;
margin-left: -12px;
background-color: green;
/* make the background color fill the content, not the border */
background-clip: content;
}
能否請您提供一些ASCII藝術展示結果應該如何?像'---- qqqqq00ddddd ---------------'? – doc
你好@doc。我已經完成了。這是第3行「結果:」基本上:Image_A +滑塊本身+ Image_B。 – Phil
使用'QHBoxLayout'是最乾淨和最簡單的。你爲什麼認爲這不是正確的方式? –