1

Im trying to create a combobox in Qgraphicsscene at the mouse position when spacebar is press. but combobox doesnt seems to be placed in the right position. Please help me. Here is my code

class ConsoleScene(QtWidgets.QGraphicsScene):
    def __init__(self, parent=None):
        super(ConsoleScene, self).__init__(parent)
        self.node_menu = None

    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_Space and not self.node_menu:
            self.node_menu = QtWidget.QCombobox()
            view = self.views()[0]
            pos = view.mapToGlobal(view.mapToGlobal(QtGui.QCursor.pos()))
            self.addWidget(self.node_menu)
            self.node_menu.move(pos.x(), pos.y())
            self.node_menu.setFocus(True)
        if event.key() == QtCore.Qt.Key_Escape and self.node_menu:
            self.node_menu.deleteLater()
            self.node_menu = None
        super(ConsoleScene, self).keyPressEvent(event)

PS:- This doesnt work windows 10, python 3.0, PyQt5 though it works on Linux, python 2.7, PyQt5

1 Answers1

0

So after going through the QCursor document, I found

pos = view.mapToGlobal(view.mapToGlobal(QtGui.QCursor.pos()))

should be

pos = view.mapToGlobal(view.**mapFromGlobal**(QtGui.QCursor.pos()))

"mapFromGlobal" translates mouse position to widget coordinates. it is also important that you "setSceneRect" or it wouldn't work