【问题标题】:PyQt4 QSlider groove color customizationPyQt4 QSlider 凹槽颜色定制
【发布时间】:2019-06-21 23:45:27
【问题描述】:

我目前正在使用 PyQt4 做一个 GUI。我的问题是:如何在索引滑块值的特定范围内为QSlider::groove:horizontal 设置不同的背景颜色?这是一个最小的工作示例:

from PyQt4 import QtCore, QtGui
#%%
data_range = [0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0]
color_range =['black','black','black','black','white','white',\
              'white','black','black','white','white','black']

class MyWidget(QtGui.QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(600,300,500,100)

        # Definition of the slider
        self.slider = QtGui.QSlider(minimum=0,\
        maximum= len(data_range)-1,\ 
        orientation=QtCore.Qt.Horizontal,\
        tickInterval=1)

        # Trying to customize it
        self.slider.setStyleSheet(\
        "QSlider::groove:horizontal {\
        border: 1px solid #999999;\
        height: 8px; \
        background: white;\
        margin: -4px 0;\
        }QSlider::handle:horizontal {\
        background-color: red;\
        border: 1px solid #5c5c5c;\
        border-radius: 0px;\
        border-color: black;\
        height: 8px;\
        width: 6px;\
        margin: -8px 2; \
        }")
       grid = QtGui.QGridLayout(self)
       hbox = QtGui.QHBoxLayout()
       hbox.addWidget(self.slider)
       grid.addLayout(hbox, 3, 0, 1, 3)


def main():
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

在这种情况下,Qslider 的凹槽应该从 0 : 4 开始为黑色,从 4 : 7 开始为白色,从 7 : 9 开始为黑色等等。

我确实尝试在qt documentation 之后使用self.slider.setStyleSheet(),但徒劳无功...

任何装饰滑块的帮助或替代想法将不胜感激。

【问题讨论】:

    标签: python python-3.x qt pyqt pyqt4


    【解决方案1】:

    我找到了问题的答案。通过使用以下函数:

    def get_groove_color(color_range):
    
        groove_color_range = 'stop:0 ' + color_range[0]
        current_color = color_range[0]
        for i in range(0,len(color_range),1):
            if color_range[i] == current_color :
                continue
            else:
                current_color = color_range[i]
                groove_color_range += ', stop:'+ str((2*i-1)/2/len(color_range)) + ' ' + color_range[i-1] + ', stop:'+ str((2*i)/2/len(color_range)) + ' ' + color_range[i] 
    
        groove_color_range += ', stop:1 ' + color_range[-1]        
        return groove_color_range
    

    我获得了 qlineargradient 插值所需的颜色范围:

    groove_color = get_groove_color(color_range)
    

    最后只需要修改self.slider.setStyleSheet()字段background-color中的凹槽颜色:

    self.slider.setStyleSheet(\
        "QSlider::groove:horizontal {\
        border: 1px solid #999999;\
        height: 8px; \
        background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, "+self.groove_color+");\
        margin: -4px 0;\
        }QSlider::handle:horizontal {\
        background-color: red;\
        border: 1px solid #5c5c5c;\
        border-radius: 0px;\
        border-color: black;\
        height: 8px;\
        width: 6px;\
        margin: -8px 2; \
        }")
    

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 2016-12-04
      • 2017-02-22
      相关资源
      最近更新 更多