is there a way to implement the slide bar with a circular hollow handle in QT?
How to realize the slider with circular hollow handle in qt
1.5k Views Asked by zhiyi At
2
There are 2 best solutions below
0
On
you can transparent background of Handel and set a border for it by stylesheet but in this way you will see a line of the slider, if you choose the background color of the slider the same as its backend you can fix it by the stylesheet, else you should write it with paint and create your custom slider.
QSlider::groove:horizontal {
border-radius: 1px;
height: 2px;
margin: 20px;
background-color: rgb(52, 59, 72);
}
QSlider::groove:horizontal:hover {
background-color: rgb(55, 62, 76);
}
QSlider::handle:horizontal {
background-color: transparent;
border: 4px solid black;
height: 22px;
width: 22px;
margin: -14px 0;
border-radius: 14px;
padding: -14px 0px;
}
ui code :
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>561</width>
<height>238</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QSlider" name="horizontalSlider">
<property name="minimumSize">
<size>
<width>0</width>
<height>70</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">QSlider::groove:horizontal {
border-radius: 1px;
height: 2px;
margin: 20px;
background-color: rgb(52, 59, 72);
}
QSlider::groove:horizontal:hover {
background-color: rgb(55, 62, 76);
}
QSlider::handle:horizontal {
background-color: white;
border: 4px solid black;
height: 22px;
width: 22px;
margin: -14px 0;
border-radius: 14px;
padding: -14px 0px;
}
</string>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>561</width>
<height>22</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>



After consulting the documentation on
QStyle, I decided to useQStyleto redrawQSliderto achieve the desired effect. Here is the implementation code.Here is an example about how to use
HollowHandleStyle:The following figure shows the final effect: