1

I have a QGraphicsView created in the QtDesigner. Now I want to connect a mouseMoveEvent to it. I understand that I must define a new class that inherits from QGraphicsView and in which mouseMoveEvent is overwritten. Here is a pretty good explanation how to do this.

So I have promoted my QGraphicsView instance in QtDesigner to the new class floorplanView. I would like to define this class in my main python file main.py as it is done in the other example:

import QtGui

class floorplanViewClass(QtGui.QGraphicsView):
    moved = pyqtSignal(QMouseEvent)

  def __init__(self, parent = None):
      super(MyView, self).__init__(parent)

  def mouseMoveEvent(self, event):
      super(MyView, self).mouseMoveEvent(event)
      print "Mouse Pointer is currently hovering at: ", event.pos()
      self.moved.emit(event)

My first question: What do I have to enter in the Header file field? Both main.h and just main give me:

File "<stdin>", line 1, in <module>
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "C:\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/Oliver/Desktop/pyqt/DRS.py", line 23, in <module>
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
File "C:\Anaconda2\lib\site-packages\PyQt4\uic\__init__.py", line 211, in loadUiType
exec(code_string.getvalue(), ui_globals)
File "<string>", line 1317, in <module>
File "DRS.py", line 23, in <module>
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
File "C:\Anaconda2\lib\site-packages\PyQt4\uic\__init__.py", line 211, in loadUiType
exec(code_string.getvalue(), ui_globals)
File "<string>", line 1317, in <module>
ImportError: cannot import name floorplanViewClass

PyQt is version 4.

Community
  • 1
  • 1
Michael Westwort
  • 914
  • 12
  • 24

1 Answers1

0

I solved the problem by trial and error:

Firstly (not the solution of the problem), myView must be replaced by the name of the newly defined class. And pyqtsignal and QMouseEvent must get the respective module names (both my fault):

from PyQt4 import QtCore, QtGui

class floorplanViewClass(QtGui.QGraphicsView):
    moved = QtCore.pyqtSignal(QtGui.QMouseEvent)

    def __init__(self, parent = None):
        super(floorplanViewClass, self).__init__(parent)

    def mouseMoveEvent(self, event):
        # call the base method to be sure the events are forwarded to the scene
        super(floorplanViewClass, self).mouseMoveEvent(event)

        print "Mouse Pointer is currently hovering at: ", event.pos()
        self.moved.emit(event)

The reason for the problem was that the Qt GUI was not loaded yet by

qtCreatorFile = "..."
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

However, these two lines must stand above the definition of the Application class. Therefore the correct order is:

class floorplanViewClass(QtGui.QGraphicsView):
    ...

qtCreatorFile = "..." # insert filename of the GUI here
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    ...
Michael Westwort
  • 914
  • 12
  • 24