如何使用样式表更改 QSlider 句柄宽度 [英] How to change QSlider handle width using stylesheet

查看:220
本文介绍了如何使用样式表更改 QSlider 句柄宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我不想在 10" 触摸屏上运行.我正在尝试使 QSlider 手柄更大,以便更容易移动.我发现了很多更改手柄宽度的示例样式表,但是当我运行代码时没有反映更改.我使用的是 PyQt5 设计器,这里是滑块的样式表.

I have an application that I wan't to run on a 10" touch screen. I am trying to make the QSlider handle larger so it will be easier to move. I have found plenty of examples of changing the handle width in the style sheet, but the changes are not being reflected when I run the code. I am using PyQt5 Designer and here is the style sheet for the slider.

QSlider::groove:horizontal {height: 10px; margin: 0 0;}
QSlider::handle:horizontal {background-color: black; border: 1px; height: 
40px; width: 40px; margin: 0 0;}

这里是主程序:

from Slidertest import Ui_MainWindow
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow 

class RPTApp(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(RPTApp, self).__init__(parent)
        self.setupUi(self)

def main():

    app = QApplication(sys.argv)
    window = RPTApp()
    window.show()
    app.exec_()    

if __name__ == '__main__':
    main()

这是滑块的设计器代码:

Here is the designer code for the slider:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(640, 480)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.PS_timeSlider = QtWidgets.QSlider(self.centralwidget)
        self.PS_timeSlider.setGeometry(QtCore.QRect(100, 100, 371, 50))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, 
QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)  
        sizePolicy.setHeightForWidth(self.PS_timeSlider.sizePolicy().
     hasHeightForWidth())
     #needed to add a return and break the line above
        self.PS_timeSlider.setSizePolicy(sizePolicy)
        self.PS_timeSlider.setBaseSize(QtCore.QSize(0, 0))
        font = QtGui.QFont()
        font.setPointSize(12)
        font.setKerning(False)
        self.PS_timeSlider.setFont(font)
        self.PS_timeSlider.setFocusPolicy(QtCore.Qt.WheelFocus)
        self.PS_timeSlider.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.PS_timeSlider.setToolTip("")
        self.PS_timeSlider.setStatusTip("")
        self.PS_timeSlider.setWhatsThis("")
        self.PS_timeSlider.setAccessibleName("")
        self.PS_timeSlider.setAccessibleDescription("")
        self.PS_timeSlider.setStyleSheet("QSlider::groove:horizontal 
{height: 10px; margin: 0 0;}\n"
"QSlider::handle:horizontal {background-color: black; border: 1px; height: 
40px; width: 40px; margin: 0 0;}\n"
"")
        self.PS_timeSlider.setMinimum(0)
        self.PS_timeSlider.setMaximum(100)
        self.PS_timeSlider.setSingleStep(1)
        self.PS_timeSlider.setPageStep(5)
        self.PS_timeSlider.setProperty("value", 50)
        self.PS_timeSlider.setSliderPosition(50)
        self.PS_timeSlider.setTracking(True)
        self.PS_timeSlider.setOrientation(QtCore.Qt.Horizontal)
        self.PS_timeSlider.setInvertedAppearance(False)
        self.PS_timeSlider.setInvertedControls(False)
        self.PS_timeSlider.setTickPosition(QtWidgets.QSlider.TicksBothSides)
        self.PS_timeSlider.setTickInterval(1)
        self.PS_timeSlider.setObjectName("PS_timeSlider")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

我已经尝试了我在搜索中找到的所有内容,但没有成功.如果您能提供任何帮助,我将不胜感激.

I have tried everything I found in my searches, but have had no luck. I would appreciate any help you can provide.

推荐答案

为了让它起作用,我发现必须在凹槽组件上设置边框.此外,为了使手柄大于凹槽,必须在手柄上设置负边距(参见 Qt 文档中的自定义 QSlider):

To get this to work, I found that a border must be set on the groove component. Also, to make the handle larger than the groove, a negative margin has to be set on the handle (see Customizing QSlider in the Qt Docs):

QSlider::groove:horizontal {
    border: 1px solid;
    height: 10px;
    margin: 0px;
    }
QSlider::handle:horizontal {
    background-color: black;
    border: 1px solid;
    height: 40px;
    width: 40px;
    margin: -15px 0px;
    }

PS:如果将滑块添加到布局中,则可能需要设置其最小高度以防止其被压缩回原始尺寸.

PS: If the slider is added to a layout, it might be necessary to set its minimum height in order to stop it being squashed back to its original dimensions.

这篇关于如何使用样式表更改 QSlider 句柄宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆