473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

(pyqt) Parameters when connecting a signal to a method?

Some code:

----------------------------------------------------------------------

def foobar(w):
print w

QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), foobar )

----------------------------------------------------------------------
How can I get this to work so "foobar" prints out the sender of the
signal (i.e. my_line_edit)?

Sep 24 '07 #1
2 2448
exhuma.twn wrote:
Some code:

----------------------------------------------------------------------

def foobar(w):
print w

QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), foobar )

----------------------------------------------------------------------
How can I get this to work so "foobar" prints out the sender of the
signal (i.e. my_line_edit)?
I _think_ there is a way to get the signals sender in Qt itself. But I'm
unsure how to get that.

Alternatively, you can use a closure to create a reference:

def foobar(source, w):
print w

def slotgen(source, slot):
def _slot(*args):
return slot(*((source,) + args))
return _slot

my_slot = slotgen(my_line_edit, foobar)

QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), my_slot )

However, be careful to keep a reference to my_slot around! Otherwise, it
will be garbage collected!

diez
Sep 24 '07 #2
On Sep 24, 4:47 pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
exhuma.twn wrote:
Some code:
----------------------------------------------------------------------
def foobar(w):
print w
QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), foobar )
----------------------------------------------------------------------
How can I get this to work so "foobar" prints out the sender of the
signal (i.e. my_line_edit)?

I _think_ there is a way to get the signals sender in Qt itself. But I'm
unsure how to get that.

Alternatively, you can use a closure to create a reference:

def foobar(source, w):
print w

def slotgen(source, slot):
def _slot(*args):
return slot(*((source,) + args))
return _slot

my_slot = slotgen(my_line_edit, foobar)

QtCore.QObject,connect( my_line_edit,
QtCore.SIGNAL("returnPressed()"), my_slot )

However, be careful to keep a reference to my_slot around! Otherwise, it
will be garbage collected!

diez
Thanks diez. This works :)
Although, I still have to digest *what* this closure does, but I will
leave this as an exercise to myself. ;)

Sep 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kim | last post by:
Hi everybody, I tried to write simple FTP program using PyQt version 3.11. The document say that QFtp has been implemented, but i keep getting error message from my simple program : My Program:...
2
by: Sibylle Koczian | last post by:
Still trying to learn PyQt from a book about several Python GUI toolkits, I seem to learn first what doesn't work. The following small script seems to work, but after closing the window I get the...
4
by: Adrian Casey | last post by:
I'm using a QTimer object to expire certain password protected GUI options in my application after 2 minutes. Currently, the timer is reset each time the user presses the 'OK' button. This is not...
10
by: Peter | last post by:
I want to draw some lines on a widget. This works ok, but when I want to redraw, the old lines are still there. How do I clear or refresh the widget, so I can draw a new set of lines? Code...
7
by: Flavio | last post by:
Hi, I have a QListview widget that allows me to store a bunch of strings in it. This strings can be visualized, sorted, selected, etc. My Problem is that I cant find a way to get the user...
3
by: Marcpp | last post by:
Hi, I'm introducing to program in python + pyqt. I have a main window that call a second window (to introduce a info with textedit) when press the second window button I need to return to the main...
2
by: Alexander Eisenhuth | last post by:
Hello pyqt users, i tried to use signal / slot across threads. With the following example I want to emit a signal when the thread loop is entered. The connected slot is never called. Why? ...
4
by: Markus Dahlbokum | last post by:
Hello, I'm trying to link python statically with qt and pyqt. I've tried this in several ways but never succeeded. At the moment the final make runs without errors but I get import errors when...
7
by: Alex Gusarov | last post by:
Hello, I have strong .NET background with C# and want to do some familiar things from it with Python, but don't know how. For example, I created form in qt designer with QCalendarWidget, translated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.