import sys
import random
from PySide2 import QtCore, QtWidgets, QtGui
# IN PROCESS
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# IN PROCESS
class SceneLabel(QtWidgets.QGraphicsTextItem):
def __init__(self,text):
super(SceneLabel, self).__init__(text)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
self.hasFocus = False
def mouseDoubleClickEvent(self, event):
print("FROM LABEL")
self.hasFocus = True
def keyPressEvent(self,event):
if(self.hasFocus==True):
print(event.key())
class MainView(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(MainView, self).__init__(parent)
def keyPressEvent(self, event):
pass
# print(event.key())
class MainScene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(MainScene, self).__init__(parent)
self.inLabel = False
def keyPressEvent(self, event):
print(event.key())
class MyWidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.scene = MainScene()
# self.st = self.scene.addText("Hello, world!")
self.st = self.scene.addItem(SceneLabel("Hello, world!"))
#self.st.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
#self.st.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
#self.st.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable)
# sl = scene label
# self.scenelabel = QtWidgets.QLineEdit()
# self.scenelabel.setText("A LINE EDIT")
# self.sl = self.scene.addWidget(self.scenelabel)
# self.sl.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
# self.sl.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable)
# self.sl.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
self.view = MainView(self.scene)
self.view.show()
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.view)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = MyWidget()
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())
我正在尝试使 QGraphicsTextItem 后代可以接收按键事件,以便在我双击它(然后它具有焦点)之后,它可以修改自己的文本。
好像我的 QGraphicsView 正在吃我的按键事件!有没有办法解决这个问题,还是我必须在 QGraphicsView 中处理它?