Results 1 to 5 of 5

Thread: [PyQt4] Trouble with event handling

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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, 11:10
  2. Key Press Event trouble
    By morraine in forum Newbie
    Replies: 6
    Last Post: 18th August 2008, 08:43
  3. Handling mouse over event for QListWidget
    By Abc in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 18:32
  4. event handling...
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 25th March 2008, 07:16
  5. QGraphicsView Handling Child Event
    By arjunasd in forum Qt Programming
    Replies: 1
    Last Post: 9th August 2007, 00: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
  •  
Qt is a trademark of The Qt Company.