Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, May 2008
defining slots with outputs

Pages: Prev | 1 | 2 | Next

Message 1 in thread

how can i create a slot that returns an int or string value.
all of examples in Qt assistant and other documents are void types. i 
want to define a slot that returns a QString for example. is it 
possible? please guide me.
thanks for your helps.

--
 [ signature omitted ] 

Message 2 in thread

Hello!
Slots are normal functions that you can call and they can return some value.

So 1) you may want to convert the slot to a function, call it and get the 
return value.
If 2) you need the Qt connection mechanism, what you're asking is not trivial, 
because you may want to connect a signal to 2 slots; in that case which 
return value are you willing to take ?

But if I understand correctly and the connection is direct (ie. not 
Qt::QueuedConnection) you can do something like this:

---- define a slot in a way that accepts an "output" parameter
class MyClass : public QObject {
	Q_OBJECT
	public slots:
		void slotNamed( int in1, const QString & in2, QString * out1 )
		{
			// set the output string to a combination of the inputs
			*out1 = QString::number( in1 ) + in2;
		}
};

---- define a signal, somewhere, with the same signature:
...
void nowPerformSomething( int, const QString &, QString * );
...

---- perform the connection, as usual:
connect( this, SIGNAL(nowPerformSomething( int, const QString &, QString * )), 
that, SLOT(slotNamed(int, const QString &, QString * )) );

---- and invoke the signal with the 'output parameter(s)'
QString result;
emit nowPerformSomething( 123, " hello", &result );
// result is now "123 hello".

Btw, if you have to do stuff like this frequently you may be in need of 
refactoring your program, since it may be a symptom that something is wrong 
in your architecture.

Regards,
Enrico Ros


On Sunday 11 May 2008 15:41:39 aliasghar.toraby@xxxxxxxxx wrote:
> how can i create a slot that returns an int or string value.
> all of examples in Qt assistant and other documents are void types. i
> want to define a slot that returns a QString for example. is it
> possible? please guide me.
> thanks for your helps.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/


 
 
 --
 Email.it, the professional e-mail, gratis per te: http://www.email.it/f
 
 Sponsor:
 Gioca con i Supereroi Marvel sul cellulare!
 Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=7752&d=11-5

--
 [ signature omitted ] 

Message 3 in thread

Enrico Ros wrote:
> If 2) you need the Qt connection mechanism, what you're asking is not trivial, 
> because you may want to connect a signal to 2 slots; in that case which 
> return value are you willing to take ?
>   
for example, a button emitting a signal and contents of a lineEdit are 
passing to a slot. and result of operation returned from slot.

--
 [ signature omitted ] 

Message 4 in thread

And where should the result go? Qt doesn't need it. If you need it, why not 
just call that function directly?

Really, I can't find any reason why slots may need to return anything to the 
caller. IMHO, you should review your architecture, something seems to be 
wrong.

On Monday 12 May 2008, aliasghar.toraby@xxxxxxxxx wrote:
> Enrico Ros wrote:
> > If 2) you need the Qt connection mechanism, what you're asking is not
> > trivial, because you may want to connect a signal to 2 slots; in that
> > case which return value are you willing to take ?
>
> for example, a button emitting a signal and contents of a lineEdit are
> passing to a slot. and result of operation returned from slot.
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 5 in thread

Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
> And where should the result go? Qt doesn't need it. If you need it, why not
> just call that function directly?
> Really, I can't find any reason why slots may need to return anything to
> the caller. IMHO, you should review your architecture, something seems to
> be wrong.

The real idea would be signals that return a value when they are emitted. But 
this doesn't make much sense, because a) signals can be connected to 0-many 
slots and other signals and b) it definitely defeats the purpose of 
weak-coupling when you rely on certain return-values...

Have fun,

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 6 in thread

Forgot something...

Am Montag, 12. Mai 2008 schrieb Arnold Krille:
> Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
> > And where should the result go? Qt doesn't need it. If you need it, why
> > not just call that function directly?
> > Really, I can't find any reason why slots may need to return anything to
> > the caller. IMHO, you should review your architecture, something seems to
> > be wrong.
> The real idea would be signals that return a value when they are emitted.
> But this doesn't make much sense, because a) signals can be connected to
> 0-many slots and other signals and b) it definitely defeats the purpose of
> weak-coupling when you rely on certain return-values...

It also breaks when connecting signals/slots across thread-boundaries...

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 7 in thread

Arnold Krille wrote:
>Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
>> And where should the result go? Qt doesn't need it. If you need it,
>> why not just call that function directly?
>> Really, I can't find any reason why slots may need to return anything
>> to the caller. IMHO, you should review your architecture, something
>> seems to be wrong.
>
>The real idea would be signals that return a value when they are
> emitted. But this doesn't make much sense, because a) signals can be
> connected to 0-many slots and other signals and b) it definitely
> defeats the purpose of weak-coupling when you rely on certain
> return-values...

You can do all of that in Qt. In fact, we even use it in the Qt public 
API.

In your signal definition:
  signals:
    void someSignal(int parameter, QList<bool> *returnValues);

Then two slots:
  public slots:
    void slotA(int parameter);
    void slotB(int, QList<bool> *);

Then, code:
    connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
SLOT(slotA(int)));
    connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
SLOT(slotB(int,QList<bool>*)));

And:
    QList<bool> retvals;
    emit someSignal(42, &retvals);

And:
void Foo::slotA(int param)
{
    /* do something */
}

void Foo::slotB(int param, QList<bool> *retvals)
{
    if (retvals)
        retvals->append(param == 42);
}

Notes:
- I passed the QList by pointer, but it could have been by reference too. 
You'd have:
    void someSignal(int parameter, QList<bool> &returnValues);

- I used a list, but you can pass any container or a simple bool* or 
bool&. If you choose not to use a list, you get only one return value and 
you don't get to know which slot set it.

- Qt examples:
http://doc.trolltech.com/4.4/qabstractsocket.html#proxyAuthenticationRequired
http://doc.trolltech.com/4.4/qnetworkaccessmanager.html#authenticationRequired

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 8 in thread

Where as all friends stressed the wrong structure in my program. i 
prefer to propound my question in another way. it is a simple problem. i 
have a lineEdit and button in main widget and a separate search class. i 
want to use button to emiting a signal. this signal should call a slot 
that execute dosearch from search class and pass lineEdit contents to 
that method. my search class has a method named "QString 
dosearch(QString)". I don't want to change it. i developed it to general 
use in future applications(may be not Qt and lack of slot technology). 
so please guide me to use that button to emiting a signal that must call 
a predefined function. this function has a QString reference. pay 
attention that search class is separated and have a private members.

Thiago Macieira wrote:
> Arnold Krille wrote:
>   
>> Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
>>     
>>> And where should the result go? Qt doesn't need it. If you need it,
>>> why not just call that function directly?
>>> Really, I can't find any reason why slots may need to return anything
>>> to the caller. IMHO, you should review your architecture, something
>>> seems to be wrong.
>>>       
>> The real idea would be signals that return a value when they are
>> emitted. But this doesn't make much sense, because a) signals can be
>> connected to 0-many slots and other signals and b) it definitely
>> defeats the purpose of weak-coupling when you rely on certain
>> return-values...
>>     
>
> You can do all of that in Qt. In fact, we even use it in the Qt public 
> API.
>
> In your signal definition:
>   signals:
>     void someSignal(int parameter, QList<bool> *returnValues);
>
> Then two slots:
>   public slots:
>     void slotA(int parameter);
>     void slotB(int, QList<bool> *);
>
> Then, code:
>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
> SLOT(slotA(int)));
>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
> SLOT(slotB(int,QList<bool>*)));
>
> And:
>     QList<bool> retvals;
>     emit someSignal(42, &retvals);
>
> And:
> void Foo::slotA(int param)
> {
>     /* do something */
> }
>
> void Foo::slotB(int param, QList<bool> *retvals)
> {
>     if (retvals)
>         retvals->append(param == 42);
> }
>
> Notes:
> - I passed the QList by pointer, but it could have been by reference too. 
> You'd have:
>     void someSignal(int parameter, QList<bool> &returnValues);
>
> - I used a list, but you can pass any container or a simple bool* or 
> bool&. If you choose not to use a list, you get only one return value and 
> you don't get to know which slot set it.
>
> - Qt examples:
> http://doc.trolltech.com/4.4/qabstractsocket.html#proxyAuthenticationRequired
> http://doc.trolltech.com/4.4/qnetworkaccessmanager.html#authenticationRequired
>
>   

--
 [ signature omitted ] 

Message 9 in thread

How about just wrapping your call to dosearch(QString) in a utility slot 
(either in your search class, or in a subclass of it), and then emitting 
the results out as a signal:

In header:

public slots:  void CallDoSearch(QString str);
signals:  void SearchResults(QString);

In implementation:

void ClassName::CallDoSearch(QString str)
{
      QString result = dosearch(str);
      emit SearchResults(str);
}

Then the search results will be available in the same detached manor as 
the call is (via signals/slots).  Just have a handler that connects to 
SLOT(SearchResults(QString)).

-Mark


aliasghar.toraby@xxxxxxxxx wrote:
> Where as all friends stressed the wrong structure in my program. i 
> prefer to propound my question in another way. it is a simple problem. 
> i have a lineEdit and button in main widget and a separate search 
> class. i want to use button to emiting a signal. this signal should 
> call a slot that execute dosearch from search class and pass lineEdit 
> contents to that method. my search class has a method named "QString 
> dosearch(QString)". I don't want to change it. i developed it to 
> general use in future applications(may be not Qt and lack of slot 
> technology). so please guide me to use that button to emiting a signal 
> that must call a predefined function. this function has a QString 
> reference. pay attention that search class is separated and have a 
> private members.
>
> Thiago Macieira wrote:
>> Arnold Krille wrote:
>>  
>>> Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
>>>    
>>>> And where should the result go? Qt doesn't need it. If you need it,
>>>> why not just call that function directly?
>>>> Really, I can't find any reason why slots may need to return anything
>>>> to the caller. IMHO, you should review your architecture, something
>>>> seems to be wrong.
>>>>       
>>> The real idea would be signals that return a value when they are
>>> emitted. But this doesn't make much sense, because a) signals can be
>>> connected to 0-many slots and other signals and b) it definitely
>>> defeats the purpose of weak-coupling when you rely on certain
>>> return-values...
>>>     
>>
>> You can do all of that in Qt. In fact, we even use it in the Qt 
>> public API.
>>
>> In your signal definition:
>>   signals:
>>     void someSignal(int parameter, QList<bool> *returnValues);
>>
>> Then two slots:
>>   public slots:
>>     void slotA(int parameter);
>>     void slotB(int, QList<bool> *);
>>
>> Then, code:
>>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
>> SLOT(slotA(int)));
>>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
>> SLOT(slotB(int,QList<bool>*)));
>>
>> And:
>>     QList<bool> retvals;
>>     emit someSignal(42, &retvals);
>>
>> And:
>> void Foo::slotA(int param)
>> {
>>     /* do something */
>> }
>>
>> void Foo::slotB(int param, QList<bool> *retvals)
>> {
>>     if (retvals)
>>         retvals->append(param == 42);
>> }
>>
>> Notes:
>> - I passed the QList by pointer, but it could have been by reference 
>> too. You'd have:
>>     void someSignal(int parameter, QList<bool> &returnValues);
>>
>> - I used a list, but you can pass any container or a simple bool* or 
>> bool&. If you choose not to use a list, you get only one return value 
>> and you don't get to know which slot set it.
>>
>> - Qt examples:
>> http://doc.trolltech.com/4.4/qabstractsocket.html#proxyAuthenticationRequired 
>>
>> http://doc.trolltech.com/4.4/qnetworkaccessmanager.html#authenticationRequired 
>>
>>
>>   
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ] 

Message 10 in thread

Oops, that should be:  emit SearchResults(result);

Sorry.
-Mark

Mark Long wrote:
> How about just wrapping your call to dosearch(QString) in a utility 
> slot (either in your search class, or in a subclass of it), and then 
> emitting the results out as a signal:
>
> In header:
>
> public slots:  void CallDoSearch(QString str);
> signals:  void SearchResults(QString);
>
> In implementation:
>
> void ClassName::CallDoSearch(QString str)
> {
>      QString result = dosearch(str);
>      emit SearchResults(str);
> }
>
> Then the search results will be available in the same detached manor 
> as the call is (via signals/slots).  Just have a handler that connects 
> to SLOT(SearchResults(QString)).
>
> -Mark
>
>
> aliasghar.toraby@xxxxxxxxx wrote:
>> Where as all friends stressed the wrong structure in my program. i 
>> prefer to propound my question in another way. it is a simple 
>> problem. i have a lineEdit and button in main widget and a separate 
>> search class. i want to use button to emiting a signal. this signal 
>> should call a slot that execute dosearch from search class and pass 
>> lineEdit contents to that method. my search class has a method named 
>> "QString dosearch(QString)". I don't want to change it. i developed 
>> it to general use in future applications(may be not Qt and lack of 
>> slot technology). so please guide me to use that button to emiting a 
>> signal that must call a predefined function. this function has a 
>> QString reference. pay attention that search class is separated and 
>> have a private members.
>>
>> Thiago Macieira wrote:
>>> Arnold Krille wrote:
>>>  
>>>> Am Montag, 12. Mai 2008 schrieb Constantin Makshin:
>>>>   
>>>>> And where should the result go? Qt doesn't need it. If you need it,
>>>>> why not just call that function directly?
>>>>> Really, I can't find any reason why slots may need to return anything
>>>>> to the caller. IMHO, you should review your architecture, something
>>>>> seems to be wrong.
>>>>>       
>>>> The real idea would be signals that return a value when they are
>>>> emitted. But this doesn't make much sense, because a) signals can be
>>>> connected to 0-many slots and other signals and b) it definitely
>>>> defeats the purpose of weak-coupling when you rely on certain
>>>> return-values...
>>>>     
>>>
>>> You can do all of that in Qt. In fact, we even use it in the Qt 
>>> public API.
>>>
>>> In your signal definition:
>>>   signals:
>>>     void someSignal(int parameter, QList<bool> *returnValues);
>>>
>>> Then two slots:
>>>   public slots:
>>>     void slotA(int parameter);
>>>     void slotB(int, QList<bool> *);
>>>
>>> Then, code:
>>>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
>>> SLOT(slotA(int)));
>>>     connect(ptr, SIGNAL(someSignal(int,QList<bool>*)), otherptr, 
>>> SLOT(slotB(int,QList<bool>*)));
>>>
>>> And:
>>>     QList<bool> retvals;
>>>     emit someSignal(42, &retvals);
>>>
>>> And:
>>> void Foo::slotA(int param)
>>> {
>>>     /* do something */
>>> }
>>>
>>> void Foo::slotB(int param, QList<bool> *retvals)
>>> {
>>>     if (retvals)
>>>         retvals->append(param == 42);
>>> }
>>>
>>> Notes:
>>> - I passed the QList by pointer, but it could have been by reference 
>>> too. You'd have:
>>>     void someSignal(int parameter, QList<bool> &returnValues);
>>>
>>> - I used a list, but you can pass any container or a simple bool* or 
>>> bool&. If you choose not to use a list, you get only one return 
>>> value and you don't get to know which slot set it.
>>>
>>> - Qt examples:
>>> http://doc.trolltech.com/4.4/qabstractsocket.html#proxyAuthenticationRequired 
>>>
>>> http://doc.trolltech.com/4.4/qnetworkaccessmanager.html#authenticationRequired 
>>>
>>>
>>>   
>>
>> -- 
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx 
>> with "unsubscribe" in the subject or the body.
>> List archive and information: http://lists.trolltech.com/qt-interest/
>>
>
> -- 
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with 
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ] 

Message 11 in thread

Am Montag, 12. Mai 2008 schrieb aliasghar.toraby@xxxxxxxxx:
> Where as all friends stressed the wrong structure in my program. i
> prefer to propound my question in another way. it is a simple problem. i
> have a lineEdit and button in main widget and a separate search class. i
> want to use button to emiting a signal. this signal should call a slot
> that execute dosearch from search class and pass lineEdit contents to
> that method. my search class has a method named "QString
> dosearch(QString)". I don't want to change it. i developed it to general
> use in future applications(may be not Qt and lack of slot technology).
> so please guide me to use that button to emiting a signal that must call
> a predefined function. this function has a QString reference. pay
> attention that search class is separated and have a private members.

That one is easy: You need a search-widget!

Simply a QWidget with a layout, the line-edit and the button (and maybe even a 
label). Connect them accordingly, add the signal "search( const QString & )" 
and there you have a generalized search-widget.

Or you start your search (when it is a fast search that can be done in 
realtime) everytime QLineEdit::textEdited( const QString& ) or 
QLineEdit::textChanged( const QString& ) fires...

Have fun,

Arnold
-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 12 in thread

Arnold Krille wrote:
> Am Montag, 12. Mai 2008 schrieb aliasghar.toraby@xxxxxxxxx:
>> Where as all friends stressed the wrong structure in my program. i
>> prefer to propound my question in another way. it is a simple problem. i
>> have a lineEdit and button in main widget and a separate search class. i
>> want to use button to emiting a signal. this signal should call a slot
>> that execute dosearch from search class and pass lineEdit contents to
>> that method. my search class has a method named "QString
>> dosearch(QString)". I don't want to change it. i developed it to general
>> use in future applications(may be not Qt and lack of slot technology).
>> so please guide me to use that button to emiting a signal that must call
>> a predefined function. this function has a QString reference. pay
>> attention that search class is separated and have a private members.
> 
> That one is easy: You need a search-widget!
> 
> Simply a QWidget with a layout, the line-edit and the button (and maybe even a 
> label). Connect them accordingly, add the signal "search( const QString & )" 
> and there you have a generalized search-widget.
> 
> Or you start your search (when it is a fast search that can be done in 
> realtime) everytime QLineEdit::textEdited( const QString& ) or 
> QLineEdit::textChanged( const QString& ) fires...

Or, when the button clicked fires, get the contents of the QLineEdit and 
call dosearch()

> 
> Have fun,
> 
> Arnold

--
 [ signature omitted ] 

Message 13 in thread

Hi,

> how can i create a slot that returns an int or string value.

See for example:
	http://doc.trolltech.com/4.4/qdialog.html#exec

> all of examples in Qt assistant and other documents are void types. i 
> want to define a slot that returns a QString for example. is it 
> possible? please guide me.

A slot is a regular function and can be called directly. In this context a 
return value makes sense, as it does for any function.

On the other hand, when a slot is called through a signal/slot connection, the 
return value doesn't make sense. Why would you want one of your slot functions 
to return a value?

-- 
 [ signature omitted ] 

Message 14 in thread

On Sunday, 11.05.2008 17:48:44 Dimitri wrote:

> On the other hand, when a slot is called through a signal/slot connection,
> the return value doesn't make sense. Why would you want one of your slot
> functions to return a value?

Look at Boost.Signals. They do provide slot return values, even in the case of 
multiple slots.

On a side note, is there a way to determine the originator of a signal, or 
pass some runtime parameters to signals? Say, I have a number of dynamically 
generated buttons and want them to call the same function but with different 
parameters when pressed?

With Boost.Signals it would have been trivial:
button.clicked().connect(boost::bind(handler, _1, someParameter))

how can I do it with Qt?

Roman.

Attachment:

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Message 15 in thread

> On a side note, is there a way to determine the originator of a signal

OK, never mind, Iâve discovered QSignalMapper.

Attachment:

Attachment: smime.p7s
Description: S/MIME cryptographic signature


Pages: Prev | 1 | 2 | Next