在pyqt6中,为什么使用qss后QSlider看不到handler?

问题描述 投票:0回答:1
class VideoProgressSlider(QSlider):
    def __init__(self, orientation: Qt.Orientation = Qt.Orientation.Horizontal):
        super().__init__(orientation)
        self.__initUI()

    def __initUI(self):
        self.setMinimumHeight(100)
        self.setStyleSheet(f'''
            QSlider::groove:horizontal {{ 
                height: 12px; 
                left: 0px; 
                right: 0px; 
                border:0px;    
                border-radius:6px;
                background:rgba(0,0,0,50);
             }} 
             QSlider::handle:horizontal{{ 
                width: 8px;
                height: 16px;
                margin-top: -8px; 
                margin-left: 1px; 
                margin-bottom: -8px; 
                margin-right: 1px;
                # core code which results in the question
                border-image: {os.path.join('../../Resources', "circle.png")};
                border: 8px solid;
            }} 
            QSlider::sub-page:horizontal{{
                border:0px;    
                border-radius:6px;
                background:qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #12b9ff, stop: 1.0 #015eea);
            }}
        ''')
        pass

    def mousePressEvent(self, event: QMouseEvent) -> None:
        # Make sure the handle will go to the position where I clicked.
        # Omitted.
        pass

正如我上面提供的,有一个新的

QSlider
修改了样式表并添加了一些功能。 这是效果图

Effect

我不知道如何解决它。
希望得到解决方案。

我尝试使用

border-image: {os.path.join('../../Resources', "circle.png")};
,
border-image: url({os.path.join('../../Resources', "circle.png")});
,
border-image: url(:/{os.path.join('../../Resources', "circle.png")});

甚至是完整路径。
但是它们都没有用。
名为
circle.png
的句柄图片是16px(我什至试过5px),路径通过终端访问验证


附加信息
这是项目树结构的一部分。

D:\...\CODE\GUI
│  ...
│
├─Dev
│  ├─Login
│  │    ...
│  │
│  ├─Modules
│  │    ...
│  │
│  ├─MyWidgets
│  │    MyMediaPlayWidget.py
│  │    VideoProgressSlider.py
│  │    ...
│  │
│  └─Utils
│       ...
│ 
└─Resources
      circle.png
      ...
python handle qtstylesheets pyqt6 qslider
1个回答
0
投票

我找到了一个解决方案,只需使用: “QSlider::handle {image: url('path');}” 而不是背景图像或边框图像。 这是我做的一个例子(我必须重新格式化绝对路径才能正确,但如果你能帮助我,我不会成功地改变图像的大小):

self.script_dir = os.path.dirname(__file__)
path_to_image = os.path.join(self.script_dir, "../../ressources/Icon/potar1.png")
abspath1 = os.path.abspath(path_to_image)
abspath1 = abspath1.replace("\\", "/")
print("chemin potar:" ,abspath1)
    
self.myslider.setStyleSheet(f"QSlider::handle {{height:227px;width:109px;image: url({abspath1});}}"
        "QSlider::add-page{}"
        "QSlider::sub-page {background-color: green;}")
© www.soinside.com 2019 - 2024. All rights reserved.