Results 1 to 5 of 5

Thread: [PyQt4] Trouble with event handling

  1. #1
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [PyQt4] Trouble with event handling

    I am connecting widgets with event handlers in PyQt and trying to find the widget that was used. I learned to do this in wxPython:
    Qt Code:
    1. button1 = wx.Button(self, -1, "Button 1")
    2. button2 = wx.Button(self, -1, "Button 2")
    3. button3 = wx.Button(self, -1, "Button 3")
    4.  
    5. button_group = (button1, button2, button3)
    6.  
    7. for entry in button_group:
    8. entry.Bind(wx.EVT_BUTTON, self.onButton)
    9.  
    10.  
    11. def onButton(self, event):
    12. eventVal = event.GetEventObject()
    13. labelVal = eventVal.GetLabel()
    14.  
    15. print "%s pressed" % labelVal
    To copy to clipboard, switch view to plain text mode 
    But I am having trouble figuring out how to do this with PyQt. This is the only way that I know how to catch events:
    Qt Code:
    1. button1 = QtGui.QPushButton("Button 1")
    2. button2 = QtGui.QPushButton("Button 2")
    3. button3 = QtGui.QPushButton("Button 3")
    4.  
    5. button_group = [button1, button2, button3]
    6.  
    7. for entry in button_group:
    8. self.connect(entry, QtCore.SIGNAL('clicked()'), self.onButton)
    9.  
    10.  
    11. def onButton(self):
    12. """
    13. I do not know what to put here.
    14. """
    15. pass
    To copy to clipboard, switch view to plain text mode 
    Last edited by Dodle; 4th May 2009 at 07:23.

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Trouble with event handling

    There is the QObject::sender method that you can use. It is not really considered good style, instead, it would be better to use a QSignalMapper.

  3. #3
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Trouble with event handling

    I think that I am close:
    Qt Code:
    1. #! /usr/bin/env python
    2. """
    3. Mapping
    4. """
    5.  
    6. import sys
    7. from PyQt4 import Qt
    8.  
    9. class MainWindow(Qt.QWidget):
    10. def __init__(self):
    11. Qt.QWidget.__init__(self)
    12.  
    13. button1 = Qt.QPushButton("Button 1")
    14. button2 = Qt.QPushButton("Button 2")
    15. button3 = Qt.QPushButton("Button 3")
    16.  
    17. self.button_mapper = Qt.QSignalMapper()
    18.  
    19. button_group = [button1, button2, button3]
    20.  
    21. for entry in button_group:
    22. self.connect(entry, Qt.SIGNAL('clicked()'), self.onButton)
    23. self.button_mapper.setMapping(entry, "blah")
    24.  
    25. sizer = Qt.QVBoxLayout(self)
    26. sizer.addWidget(button1, 1)
    27. sizer.addWidget(button2, 1)
    28. sizer.addWidget(button3, 1)
    29.  
    30. def onButton(self):
    31. eventVal = self.button_mapper.mapping("blah")
    32. labelVal = eventVal."""What to put here"""()
    33. print labelVal
    34.  
    35. app = Qt.QApplication(sys.argv)
    36. frame = MainWindow()
    37. frame.show()
    38. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    I just don't know how to get the label off of the button.

  4. #4
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Trouble with event handling

    Okay I figured out what to put in the line I was having trouble with before:
    Qt Code:
    1. labelVal = eventVal.text()
    To copy to clipboard, switch view to plain text mode 

    But every button I press prints "Button 3". I guessing because it was the last item to be mapped.

  5. #5
    Join Date
    May 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [PyQt4] Trouble with event handling

    I got some help with this over at ubuntuforums.org:
    Quote Originally Posted by regomodo
    I believe you are looking for the objectname for a widget.

    Say you have a_button = QToolbutton() and have set it's objectName with a_button.setObjectName("spam")

    You have also slotted(not sure that's the right term) the button to a function like:

    self.connect(a_button, SIGNAL("clicked()"), self.eggs)

    The function to find the objectName of the button would look like this.

    def eggs(self):
    print self.sender().objectName()


    Hope that's what you wanted.
    I think that this method will work best for what I want to accomplish. Thanks for your help.

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 12:10
  2. Key Press Event trouble
    By morraine in forum Newbie
    Replies: 6
    Last Post: 18th August 2008, 09:43
  3. Handling mouse over event for QListWidget
    By Abc in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 19:32
  4. event handling...
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 08:16
  5. QGraphicsView Handling Child Event
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 01: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.