我正在尝试使用 PyQt5 和 Qt Designer 制作一个简单的速度计。由于速度计箭头的旋转,我遇到了问题self.arrow
。
这是代码:
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.speedSlider.valueChanged.connect(self.valuechange)
scene = QGraphicsScene()
scene.setSceneRect(0, 0, self.width(), self.height())
self.pixmap = QPixmap(":/img/img/speed_arrow.png")
self.arrow = scene.addPixmap(self.pixmap)
graphicsView = QGraphicsView(scene, self.ui.centralwidget)
graphicsView.setStyleSheet( "background-image:url(:/img/img/central1.png);\n"
"background-repeat:no-repeat;\n"
"background-position: center;\n"
"\n"
"border:none;")
self.ui.gridLayout.addWidget(graphicsView)
def valuechange(self):
angel = self.ui.speedSlider.value()
xform = QTransform()
xform.rotate(angel)
xformed_pixmap = self.pixmap.transformed(xform, Qt.SmoothTransformation)
self.arrow.setPixmap(xformed_pixmap)
#print(self.arrow.sceneBoundingRect())
我没有在代码本身中绘制速度计箭头,而是加载了一个 .
我还添加了一个滑块self.ui.speedSlider
,应该用它来确定旋转角度。
当我移动滑块时,实际上实现了箭头的旋转。问题是箭头不绕其中心旋转。
如何固定旋转中心,使其始终位于箭头的中心self.arrow
?
这是 Ui_MainWindow 代码:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
MainWindow.setStyleSheet("background-image:url(:/img/img/bgd.png);\n"
"possition:center;")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.speedSlider = QtWidgets.QSlider(self.centralwidget)
self.speedSlider.setMaximum(99)
self.speedSlider.setOrientation(QtCore.Qt.Horizontal)
self.speedSlider.setObjectName("speedSlider")
self.gridLayout.addWidget(self.speedSlider, 1, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
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"))
箭
中央1