2017-10-17 85 views
1

我有一個應用程序,我不想在10英寸觸摸屏上運行,我試圖讓QSlider手柄更大,因此移動起來更容易。在樣式表改變手柄寬度的例子,但所做的更改不會被反射,當我運行的代碼。我使用PyQt5設計,這裏是樣式表滑塊。如何使用樣式表更改QSlider手柄寬度

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() 

以下是滑塊設計器代碼:

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")) 

我試過在我的搜索中找到的所有東西,但都沒有運氣。我希望你能提供任何幫助。

回答

0

爲了得到這個工作,我發現必須在凹槽部件上設置邊框。此外,爲了使處理比槽大,負保證金必須在手柄上(見Customizing QSlider在Qt的文檔)設置:

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:如果滑塊被添加到佈局,它可能有必要設置其最小高度,以阻止其被壓扁回原始尺寸。

+0

謝謝。自從昨天以來,我一直在想這件事。 – Mike