【问题标题】:PyQt4/Qt: Set orientation of QDial (minimum value at the top)PyQt4/Qt:设置 QDial 的方向(最小值在顶部)
【发布时间】:2013-08-28 11:23:30
【问题描述】:

我在 Windows 上使用 PyQt4 和 Python 2.7,对于音乐播放器,我决定将 Phonon.SeekSlider 替换为 QDial
我的问题是,QDial 的最小值始终位于表盘的底部,但我希望它位于顶部(值仍顺时针增加)。我试过setInvertedAppearance()setInvertedControls(),但这只是水平翻转表盘,setOrientation()也没有帮助。
我的问题:有没有办法指定一个角度来定位表盘上的最小值或垂直翻转整个表盘?

【问题讨论】:

    标签: qt pyqt pyqt4 qdial


    【解决方案1】:

    我不相信有办法直接做到这一点。但是,您可以将QDial 放在QGraphicsView 中,并将其作为场景中的一个项目进行旋转。像这样的东西应该可以工作:

    import sys
    from PyQt4 import QtGui, QtCore
    
    class RotatableView(QtGui.QGraphicsView):
        def __init__(self, item, rotation=0):
            QtGui.QGraphicsView.__init__(self)
            scene = QtGui.QGraphicsScene(self)
            self.setScene(scene)
            graphics_item = scene.addWidget(item)
            graphics_item.rotate(rotation)
    
            # make the QGraphicsView invisible
            self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            self.setFixedHeight(item.height())
            self.setFixedWidth(item.width())
            self.setStyleSheet("border: 0px")
    
    class DialExample(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            dial = QtGui.QDial()
            dial.setNotchesVisible(True)
            label = QtGui.QLabel('0')
            dial.valueChanged.connect(label.setNum)
    
            layout = QtGui.QVBoxLayout()
            layout.addWidget(RotatableView(dial, 180))
            layout.addWidget(label)
            self.setLayout(layout)
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        dialexample = DialExample()
        dialexample.show()
        sys.exit(app.exec_())
    

    【讨论】:

      【解决方案2】:

      Hackyday 给出一个QgraphicsView 的答案,这里我给你一个pyqt 的解决方案,不使用QgraphicsView。 你可以直接在 python 上使用 if/else 声明。

      你创建了你的qDial,我想你想要一个表盘作为指南针(0 - 360°),我想你在qSpinBox上写了qDial的输出):

      <widget class="QDial" name="dial">
       <property name="geometry">
        <rect>
         <x>20</x>
         <y>150</y>
         <width>101</width>
         <height>111</height>
        </rect>
       </property>
       <property name="mouseTracking">
        <bool>false</bool>
       </property>
       <property name="acceptDrops">
        <bool>false</bool>
       </property>
       <property name="layoutDirection">
        <enum>Qt::LeftToRight</enum>
       </property>
       <property name="minimum">
        <number>0</number>
       </property>
       <property name="maximum">
        <number>360</number>
       </property>
       <property name="singleStep">
        <number>0</number>
       </property>
       <property name="pageStep">
        <number>0</number>
       </property>
       <property name="value">
        <number>135</number>
       </property>
       <property name="sliderPosition">
        <number>135</number>
       </property>
       <property name="orientation">
        <enum>Qt::Horizontal</enum>
       </property>
       <property name="invertedAppearance">
        <bool>false</bool>
       </property>
       <property name="invertedControls">
        <bool>false</bool>
       </property>
       <property name="wrapping">
        <bool>true</bool>
       </property>
       <property name="notchesVisible">
        <bool>false</bool>
       </property>
      </widget>
      
      
      <widget class="QSpinBox" name="oAzimuth">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>110</y>
         <width>52</width>
         <height>30</height>
        </rect>
       </property>
       <property name="maximum">
        <number>360</number>
       </property>
       <property name="value">
        <number>315</number>
       </property>
      </widget>
      

      如果您想将滑块的默认位置设置为 315°,请按照我的示例将其设置为 135°。为什么 ?因为 0° 和 180° 之间的所有度数都必须转换为 +180°,而 181° 和 360° 之间的度数必须转换为 -180°。

      所以在pyqt 上,在你的file.py 上,你可以编写if/else 语句以在function 内自动更正:

      def Edit_Slider(self):
          if self.dockwidget.spinbox.value() > 180:
               self.dockwidget.dial.setValue(self.dockwidget.spinbox.value()-180)
          else:
               self.dockwidget.dial.setValue(self.dockwidget.spinbox.value()+180)
      
      def Edit_Spinbox(self):
          if self.dockwidget.dial.value() > 180:
               self.dockwidget.spinbox.setValue(self.dockwidget.dial.value()-180)
          else:
               self.dockwidget.spinbox.setValue(self.dockwidget.dial.value()+180)
      

      并在您移动滑块或 SpinValue 时自动编辑此更改:

      self.dockwidget.spinbox.valueChanged.connect(self.Edit_Slider)
      self.dockwidget.dial.valueChanged.connect(self.Edit_Spinbox)
      

      【讨论】:

      • 将值转换为正确工作的数学被成功使用,谢谢!
      【解决方案3】:

      您也可以在 linedit 中使用 qdial 值。 由于 QDial 仅适用于整数,通过使用以下命令,您可以使用浮点中的任何值。 self.dial_object = QtGui.QDial(self.centralwidget) self.dial_object.setNotchesVisible(True) self.dial_object.setGeometry(QtCore.QRect(15,110,45,45)) self.dial_object.setMinimum(50) 这将拨号最小值设置为 0.5 self.dial_object.setMaximum(100) 这将拨号最大值设置为 1 self.dial_object.setSingleStep(10) 这将拨号分辨率值设置为 .01 self.dial_object.setValue(0) 这会将拨号起始值设置为 0 self.dial_object.valueChanged.connect(self.dvalue1) 在哪里 def dvalue1(self): self.lineEdit_object.setText(str(float(self.dial_object.value())*0.01)) 这会将值更改为浮点数到字符串类型(在 setText 中使用)。 0.01 决定表盘的分辨率。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        • 2021-04-09
        • 2012-03-18
        • 2018-07-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多