PDA

View Full Version : Qt Signals and Slots with Qt Designer



JeffMGreg
23rd September 2011, 20:04
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.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(graphicsView Widget)

def retranslateUi(self, graphicsViewWidget):
graphicsViewWidget.setWindowTitle(QtGui.QApplicati on.translate("graphicsViewWidget", "Form", None, QtGui.QApplication.UnicodeUTF8))


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

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


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.

wysota
23rd September 2011, 21:09
But you don't do anything with the graphics view. You override the event handler for the QMainWindow object and not the graphics view.

JeffMGreg
23rd September 2011, 22:44
I know, but I don't see how I can do anything with graphics view without altering the code generated from qt designer.

wysota
23rd September 2011, 22:59
When in Designer, right click your graphics view widget and choose "Promote to...", then input the name of your custom graphics view based class. Then PyUIC should generate code where the standard graphics view object is replaced by an instance of the class you have chosen.