[Qt-interest] Passing arguments to SLOT()

Andre Somers andre at familiesomers.nl
Mon Jul 5 12:08:57 CEST 2010


On 5-7-2010 11:54, Nishant Sharma wrote:
> Hi,
> I have a function in which I have a QString; say, QString abc = "QT"
> Now, in this function, I have a connect() statement.
> In the SLOT function, that I call, I want to pass the variable, abc.
> How do I achieve the same?
>
Hi,

It looks like you may be misunderstanding the function of connect.
Connect creates a connection between a signal and a slot. Connecting 
them causes the slot to be called when the signal is emitted. Usually, 
you create such connections at the moment of initialization of a class. 
Then, when you have an actual event you want to signal (with or without 
arguments such as your "Qt" QString), you emit the signal and be assured 
the connected slot(s) will be called.

So, to pass the value of abc through the signal/slot mechanism:

1) define the signal and the slot in the classes you want them in
2) implement the slot function to do whatever you want
2) connect the signal and the slot ONCE.
3) emit the signal with the correct argument when that is appropriate

The connect statement probably has no place in the function where you 
are dealing with the value itself.

Note that it could be perfectly valid in your case to just do a direct 
method call instead of using signal/slot semantics. It all depends on 
the design of your application.

André



-- 
Nokia Certified Qt Developer.
Interested in Qt related job offers or freelance Qt consulting work.




More information about the Qt-interest-old mailing list