我想在 PyQt 中使用 QGraphicsScene 旋转一个小部件(QLineEdit 和 QPushButton)
I would like to rotate a widget using QGraphicsScene in PyQt (QLineEdit and QPushButton)
我想在pyqt5中旋转一个小部件,我已经开发了这段代码但是它不起作用。角度不会更新,并且 returns 为 False。有谁知道如何更新这个角度以使小部件旋转?如果有人能帮忙,谢谢。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
robotx=200
roboty=100
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Awesome Rotating Widget")
self.setGeometry(0, 0, 640, 480)
self.R=Robot()
self.angle=0
self.AngleLineEdit=QLineEdit(self)
self.AngleLineEdit.setGeometry(50,50,160,35)
self.AngleLineEdit.setStyleSheet("background-color: rgba(50,50,50,20); color:black; font-weight: bold; font-size: 8pt; font-family: Helvetica; border-radius:5px;")
self.AcceptButton=QPushButton(self)
self.AcceptButton.setText("Accept")
self.AcceptButton.setGeometry(50,100,160,35)
self.AcceptButton.setStyleSheet("QPushButton{color:black; font-weight: bold; font-size: 8pt; font-family: Helvetica; background-color:rgb(255,255,255,20); border-radius:5px}""QPushButton:hover{background-color : rgb(255,255,255,100);}")
container = RotatableContainer(self,self.R, 0)
container.move(robotx,roboty)
container.resize(150,150)
container.setStyleSheet("background-color:transparent;")
self.AcceptButton.clicked.connect(lambda: self.RotateWidget())
self.AcceptButton.clicked.connect(container.rotate)
self.show()
def RotateWidget(self):
self.angle=int(self.AngleLineEdit.text())
print(self.angle)
class RotatableContainer(QGraphicsView):
def __init__(self, parent, widget, angle):
super().__init__(parent)
scene = QGraphicsScene(self)
self.setScene(scene)
self.proxy = QGraphicsProxyWidget()
self.proxy.setWidget(widget)
self.proxy.setTransformOriginPoint(self.proxy.boundingRect().center())
self.proxy.setRotation(angle)
scene.addItem(self.proxy)
def rotate(self, angle):
print(angle)
self.proxy.setRotation(angle)
class Robot(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0,0,100,100)
def paintEvent(self, event):
p=QPainter(self)
r=QRect(0,0,99,99)
c=QColor(0,0,0)
p.setBrush(QBrush(c))
p.drawRect(r)
app = QApplication([])
window = MainWindow()
app.exec_()
我看到了如何使用 qslider 旋转小部件的示例,但我不知道如何使用 QLineEdit 和 QPushButton 对其进行调整。
clicked
的第一个参数始终是其检查状态,对于非 checkable/checked 按钮默认为 False
。
由于您已将信号连接到 rotate
函数,因此参数是按钮信号的参数,并且由于 False
也意味着 0
,您实际上是在做self.proxy.setRotation(0)
.
将容器设置为实例属性并从那里调用其 rotate
函数:
class MainWindow(QMainWindow):
def __init__(self):
# ...
self.container = RotatableContainer(self,self.R, 0)
self.container.move(robotx,roboty)
self.container.resize(150,150)
self.container.setStyleSheet("background-color:transparent;")
self.AcceptButton.clicked.connect(self.RotateWidget)
self.show()
def RotateWidget(self):
angle = self.AngleLineEdit.text()
if angle.isdigit():
self.angle = angle
self.container.rotate(self.angle)
注意:您应该始终使用 layout managers, and only classes and constants should have capitalized names (see the Style Guide for Python Code).
我想在pyqt5中旋转一个小部件,我已经开发了这段代码但是它不起作用。角度不会更新,并且 returns 为 False。有谁知道如何更新这个角度以使小部件旋转?如果有人能帮忙,谢谢。
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
robotx=200
roboty=100
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My Awesome Rotating Widget")
self.setGeometry(0, 0, 640, 480)
self.R=Robot()
self.angle=0
self.AngleLineEdit=QLineEdit(self)
self.AngleLineEdit.setGeometry(50,50,160,35)
self.AngleLineEdit.setStyleSheet("background-color: rgba(50,50,50,20); color:black; font-weight: bold; font-size: 8pt; font-family: Helvetica; border-radius:5px;")
self.AcceptButton=QPushButton(self)
self.AcceptButton.setText("Accept")
self.AcceptButton.setGeometry(50,100,160,35)
self.AcceptButton.setStyleSheet("QPushButton{color:black; font-weight: bold; font-size: 8pt; font-family: Helvetica; background-color:rgb(255,255,255,20); border-radius:5px}""QPushButton:hover{background-color : rgb(255,255,255,100);}")
container = RotatableContainer(self,self.R, 0)
container.move(robotx,roboty)
container.resize(150,150)
container.setStyleSheet("background-color:transparent;")
self.AcceptButton.clicked.connect(lambda: self.RotateWidget())
self.AcceptButton.clicked.connect(container.rotate)
self.show()
def RotateWidget(self):
self.angle=int(self.AngleLineEdit.text())
print(self.angle)
class RotatableContainer(QGraphicsView):
def __init__(self, parent, widget, angle):
super().__init__(parent)
scene = QGraphicsScene(self)
self.setScene(scene)
self.proxy = QGraphicsProxyWidget()
self.proxy.setWidget(widget)
self.proxy.setTransformOriginPoint(self.proxy.boundingRect().center())
self.proxy.setRotation(angle)
scene.addItem(self.proxy)
def rotate(self, angle):
print(angle)
self.proxy.setRotation(angle)
class Robot(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0,0,100,100)
def paintEvent(self, event):
p=QPainter(self)
r=QRect(0,0,99,99)
c=QColor(0,0,0)
p.setBrush(QBrush(c))
p.drawRect(r)
app = QApplication([])
window = MainWindow()
app.exec_()
我看到了如何使用 qslider 旋转小部件的示例,但我不知道如何使用 QLineEdit 和 QPushButton 对其进行调整。
clicked
的第一个参数始终是其检查状态,对于非 checkable/checked 按钮默认为 False
。
由于您已将信号连接到 rotate
函数,因此参数是按钮信号的参数,并且由于 False
也意味着 0
,您实际上是在做self.proxy.setRotation(0)
.
将容器设置为实例属性并从那里调用其 rotate
函数:
class MainWindow(QMainWindow):
def __init__(self):
# ...
self.container = RotatableContainer(self,self.R, 0)
self.container.move(robotx,roboty)
self.container.resize(150,150)
self.container.setStyleSheet("background-color:transparent;")
self.AcceptButton.clicked.connect(self.RotateWidget)
self.show()
def RotateWidget(self):
angle = self.AngleLineEdit.text()
if angle.isdigit():
self.angle = angle
self.container.rotate(self.angle)
注意:您应该始终使用 layout managers, and only classes and constants should have capitalized names (see the Style Guide for Python Code).