I'm using Qt Designer to make a simple widget with a QGraphicsView in it. My problem is when I write the main program and try to use the QGraphicsView from the gui file, I can't get any events.
Here's an example of what I'm trying to do. This code is generated from Qt Designer:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8
= QtCore.
QString.
fromUtf8except AttributeError:
_fromUtf8 = lambda s: s
class Ui_graphicsViewWidget(object):
def setupUi(self, graphicsViewWidget):
graphicsViewWidget.setObjectName(_fromUtf8("graphicsViewWidget"))
graphicsViewWidget.resize(400, 300)
graphicsViewWidget.setMouseTracking(True)
self.
graphicsView.
setGeometry(QtCore.
QRect(70,
40,
256,
192)) self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.retranslateUi(graphicsViewWidget)
QtCore.
QMetaObject.
connectSlotsByName(graphicsViewWidget
)
def retranslateUi(self, graphicsViewWidget):
graphicsViewWidget.
setWindowTitle(QtGui.
QApplication.
translate("graphicsViewWidget",
"Form", None, QtGui.
QApplication.
UnicodeUTF8))
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_graphicsViewWidget(object):
def setupUi(self, graphicsViewWidget):
graphicsViewWidget.setObjectName(_fromUtf8("graphicsViewWidget"))
graphicsViewWidget.resize(400, 300)
graphicsViewWidget.setMouseTracking(True)
self.graphicsView = QtGui.QGraphicsView(graphicsViewWidget)
self.graphicsView.setGeometry(QtCore.QRect(70, 40, 256, 192))
self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
self.retranslateUi(graphicsViewWidget)
QtCore.QMetaObject.connectSlotsByName(graphicsViewWidget)
def retranslateUi(self, graphicsViewWidget):
graphicsViewWidget.setWindowTitle(QtGui.QApplication.translate("graphicsViewWidget", "Form", None, QtGui.QApplication.UnicodeUTF8))
To copy to clipboard, switch view to plain text mode
This is my part of the code:
#!/usr/bin/python -d
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from gui import Ui_graphicsViewWidget
def __init__(self, parent=None):
self.ui = Ui_graphicsViewWidget()
self.ui.setupUi(self)
self.ui.graphicsView.setFocus(True)
def mousePressEvent(self, event):
print("mouse pressed")
if __name__ == "__main__":
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
#!/usr/bin/python -d
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from gui import Ui_graphicsViewWidget
class MyForm(QMainWindow):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.ui = Ui_graphicsViewWidget()
self.ui.setupUi(self)
self.ui.graphicsView.setFocus(True)
def mousePressEvent(self, event):
print("mouse pressed")
if __name__ == "__main__":
app = QApplication(sys.argv)
myapp = MyForm()
myapp.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Basically what I want here is when I click on the QGraphicsView, I want to capture the mousePressEvent and print something.
I've tried everything I can think of and searched all over the wed, still can't find a solution.
I can accomplish this by writing the whole gui out by hand but I want to know how to do this with Qt Designer.
Thanks for your help.
Bookmarks