trayres
trayres

Reputation: 532

QGraphicsTextItem receive keypresses

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_())

I'm trying to make it so that a QGraphicsTextItem descendent can receive keypress events, so that after I double click on it (and it then has focus), it can then modify its own text.

It seems like my QGraphicsView is eating my keypress events! Is there a way around that, or do I have to handle it in the QGraphicsView?

Upvotes: 1

Views: 254

Answers (1)

eyllanesc
eyllanesc

Reputation: 243965

Do not reinvent the wheel since QGraphicsTextItem already implements the functionality of being editable with the click event by enabling the Qt::TextEditable flag:

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_())

Upvotes: 1

Related Questions