Results 1 to 4 of 4

Thread: Qt Signals and Slots with Qt Designer

  1. #1
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Qt products
    Platforms
    Unix/X11 Windows

    Default Qt Signals and Slots with Qt Designer

    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:
    Qt Code:
    1. from PyQt4 import QtCore, QtGui
    2.  
    3. try:
    4. _fromUtf8 = QtCore.QString.fromUtf8
    5. except AttributeError:
    6. _fromUtf8 = lambda s: s
    7.  
    8. class Ui_graphicsViewWidget(object):
    9. def setupUi(self, graphicsViewWidget):
    10. graphicsViewWidget.setObjectName(_fromUtf8("graphicsViewWidget"))
    11. graphicsViewWidget.resize(400, 300)
    12. graphicsViewWidget.setMouseTracking(True)
    13. self.graphicsView = QtGui.QGraphicsView(graphicsViewWidget)
    14. self.graphicsView.setGeometry(QtCore.QRect(70, 40, 256, 192))
    15. self.graphicsView.setObjectName(_fromUtf8("graphicsView"))
    16.  
    17. self.retranslateUi(graphicsViewWidget)
    18. QtCore.QMetaObject.connectSlotsByName(graphicsViewWidget)
    19.  
    20. def retranslateUi(self, graphicsViewWidget):
    21. 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:

    Qt Code:
    1. #!/usr/bin/python -d
    2.  
    3. import sys
    4. from PyQt4.QtCore import *
    5. from PyQt4.QtGui import *
    6. from gui import Ui_graphicsViewWidget
    7.  
    8. class MyForm(QMainWindow):
    9.  
    10. def __init__(self, parent=None):
    11. QWidget.__init__(self, parent)
    12. self.ui = Ui_graphicsViewWidget()
    13. self.ui.setupUi(self)
    14. self.ui.graphicsView.setFocus(True)
    15.  
    16. def mousePressEvent(self, event):
    17. print("mouse pressed")
    18.  
    19.  
    20. if __name__ == "__main__":
    21. app = QApplication(sys.argv)
    22. myapp = MyForm()
    23. myapp.show()
    24. 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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Signals and Slots with Qt Designer

    But you don't do anything with the graphics view. You override the event handler for the QMainWindow object and not the graphics view.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    6
    Thanks
    1
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Qt Signals and Slots with Qt Designer

    I know, but I don't see how I can do anything with graphics view without altering the code generated from qt designer.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt Signals and Slots with Qt Designer

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    JeffMGreg (25th September 2011)

Similar Threads

  1. signals and slots and menus problem in designer
    By hollowhead in forum Qt Tools
    Replies: 0
    Last Post: 5th March 2010, 18:31
  2. Replies: 2
    Last Post: 28th August 2009, 08:12
  3. Qt Designer 4.4.1 Signals and Slots
    By M4d_Us3r in forum Qt Tools
    Replies: 1
    Last Post: 13th October 2008, 22:03
  4. Replies: 12
    Last Post: 23rd June 2008, 09:05
  5. regarding signals/slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 4th October 2007, 10:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.