我在WindowsPython2.7上使用PyQt4,对于音乐播放器,我决定用QDial代替Phonon.SeekSlider。
我的问题是,QDial的最小值总是在刻度盘的底部,但我希望它位于顶部(值仍按顺时针方向增加)。我尝试了setInvertedAppearance()和setInvertedControls(),但是这只是水平地翻转拨号盘,setOrientation()也没有帮助。
我的问题是:有什么方法可以指定一个角度来定位刻度盘上的最小值,还是垂直翻转整个刻度盘?
发布于 2013-08-30 22:55:15
我不相信有办法直接做到这一点。但是,您可以将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_())发布于 2017-10-19 09:49:22
Hackyday给出了一个QgraphicsView答案,这里我给您一个不使用QgraphicsView的pyqt解决方案。您可以直接在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)发布于 2018-12-12 04:26:14
您还可以在行编辑中使用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) --将拨号最小值设置为.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决定拨号的分辨率。
https://stackoverflow.com/questions/18486501
复制相似问题