0
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 中处理它?

4

1 回答 1

0

不要重新发明轮子,因为 QGraphicsTextItem 已经通过启用Qt::TextEditable标志实现了点击事件可编辑的功能:

from PySide2 import QtCore, QtGui, QtWidgets 


class SceneLabel(QtWidgets.QGraphicsTextItem):
    def __init__(self, text, parent=None):
        super().__init__(text, parent)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
        self.setTextInteractionFlags(QtCore.Qt.TextEditable)


class MainView(QtWidgets.QGraphicsView):
    pass


class MainScene(QtWidgets.QGraphicsScene):
    pass


class MyWidget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scene = MainScene(self)
        self.st = SceneLabel("Hello, world!")
        self.scene.addItem(self.st)
        self.view = MainView(self.scene)

        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.view)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    widget = MyWidget()
    widget.resize(800, 600)
    widget.show()

    sys.exit(app.exec_())
于 2019-09-01T23:24:05.603 回答