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

Qt-interest Archive, August 2006
small signal/slot problem


Message 1 in thread

Hello,
I have a slot in my class which expects integers as parameter and would
like to connect this to a button. What should happen on clicking the
button is that the text of the button should be read and converted to an
integer and then it should be sent to the slot. E.g.:

void FrameWork::test(int) {...}

int tmp = left_click->text().toInt();
connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));

Unfortunately, this does not work because of the type-safety of signals
and slots such that I could only write something like

connect(button01,SIGNAL(clicked()),this,SLOT(test()));

but here I would miss the needed parameter. Is there a way of connecting
the two as described above or do i have to derive from QPushButton and
overwrite the signal clicked() such that it sends its contents as
integer?

Thanks a lot for your suggestions and help
Tim

--
 [ signature omitted ] 

Message 2 in thread

> From: Tim Kietzmann [mailto:tkietzma@xxxxxx]
> Sent: Tuesday, August 29, 2006 11:08 AM
> 
> Hello,
> I have a slot in my class which expects integers as parameter and
would
> like to connect this to a button. What should happen on clicking the
> button is that the text of the button should be read and converted to
an
> integer and then it should be sent to the slot. E.g.:
> 
> void FrameWork::test(int) {...}
> 
> int tmp = left_click->text().toInt();
> connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));
> 
> Unfortunately, this does not work because of the type-safety of
signals
> and slots such that I could only write something like
> 
> connect(button01,SIGNAL(clicked()),this,SLOT(test()));
> 
> but here I would miss the needed parameter. Is there a way of
connecting
> the two as described above or do i have to derive from QPushButton and
> overwrite the signal clicked() such that it sends its contents as
> integer?
> 

I don't know of a general solution to your problem, but considering this
specific case, you could use sender() in your test slot.

void FrameWork::test()
{
   //adding type safety recommended
   int t = static_cast<QAbstractButton*>(sender())->text().toInt();
   //...
}

Tom
-----------------------------------------------------------------------
DISCLAIMER:  Information contained in this message and/or attachment(s) may contain confidential information of Zetec, Inc. If you have received this transmission in error, please notify the sender by return email.

Ce courrier électronique est confidentiel et protégé. L'expéditeur ne renonce pas aux droits et obligations qui s'y rapportent. Toute diffusion, utilisation ou copie de ce message ou des renseignements qu'il contient par une personne autre que le (les) destinataire(s) désigné(s) est interdite. Si vous recevez ce courrier électronique par erreur, veuillez m'en aviser immédiatement, par retour de courrier électronique ou par un autre moyen.
-----------------------------------------------------------------------

--
 [ signature omitted ] 

Message 3 in thread

On Tue, 2006-08-29 at 11:23 -0700, Tom Matelich wrote:
> > From: Tim Kietzmann [mailto:tkietzma@xxxxxx]
> > Sent: Tuesday, August 29, 2006 11:08 AM
> > 
> > Hello,
> > I have a slot in my class which expects integers as parameter and
> would
> > like to connect this to a button. What should happen on clicking the
> > button is that the text of the button should be read and converted to
> an
> > integer and then it should be sent to the slot. E.g.:
> > 
> > void FrameWork::test(int) {...}
> > 
> > int tmp = left_click->text().toInt();
> > connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));
> > 
> > Unfortunately, this does not work because of the type-safety of
> signals
> > and slots such that I could only write something like
> > 
> > connect(button01,SIGNAL(clicked()),this,SLOT(test()));
> > 
> > but here I would miss the needed parameter. Is there a way of
> connecting
> > the two as described above or do i have to derive from QPushButton and
> > overwrite the signal clicked() such that it sends its contents as
> > integer?
> > 
> 
> I don't know of a general solution to your problem, but considering this
> specific case, you could use sender() in your test slot.
> 
> void FrameWork::test()
> {
>    //adding type safety recommended
>    int t = static_cast<QAbstractButton*>(sender())->text().toInt();
>    //...
> }

Thanks for the tip, but the slot is called by several different objects,
such that I would have to check whether the sender really was a
QAbstractButton or whether it was a QComboBox. Is there a smart (Qt-)way
of checking this? 

Thanks once more
Tim


--
 [ signature omitted ] 

Message 4 in thread

Use QSignalMapper.

Brad

On Aug 29, 2006, at 2:34 PM, Tim Kietzmann wrote:

> On Tue, 2006-08-29 at 11:23 -0700, Tom Matelich wrote:
>>> From: Tim Kietzmann [mailto:tkietzma@xxxxxx]
>>> Sent: Tuesday, August 29, 2006 11:08 AM
>>>
>>> Hello,
>>> I have a slot in my class which expects integers as parameter and
>> would
>>> like to connect this to a button. What should happen on clicking the
>>> button is that the text of the button should be read and  
>>> converted to
>> an
>>> integer and then it should be sent to the slot. E.g.:
>>>
>>> void FrameWork::test(int) {...}
>>>
>>> int tmp = left_click->text().toInt();
>>> connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));
>>>
>>> Unfortunately, this does not work because of the type-safety of
>> signals
>>> and slots such that I could only write something like
>>>
>>> connect(button01,SIGNAL(clicked()),this,SLOT(test()));
>>>
>>> but here I would miss the needed parameter. Is there a way of
>> connecting
>>> the two as described above or do i have to derive from  
>>> QPushButton and
>>> overwrite the signal clicked() such that it sends its contents as
>>> integer?
>>>
>>
>> I don't know of a general solution to your problem, but  
>> considering this
>> specific case, you could use sender() in your test slot.
>>
>> void FrameWork::test()
>> {
>>    //adding type safety recommended
>>    int t = static_cast<QAbstractButton*>(sender())->text().toInt();
>>    //...
>> }
>
> Thanks for the tip, but the slot is called by several different  
> objects,
> such that I would have to check whether the sender really was a
> QAbstractButton or whether it was a QComboBox. Is there a smart  
> (Qt-)way
> of checking this?
>
> Thanks once more
> Tim
>
>
> --
> 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 5 in thread

On Tue, 2006-08-29 at 15:03 -0400, Brad Howes wrote:
> Use QSignalMapper.
> 
Thanks a lot, I did not know of this class before.

Tim

> 
> Brad
> 
> On Aug 29, 2006, at 2:34 PM, Tim Kietzmann wrote:
> 
> > On Tue, 2006-08-29 at 11:23 -0700, Tom Matelich wrote:
> > > > From: Tim Kietzmann [mailto:tkietzma@xxxxxx]
> > > > Sent: Tuesday, August 29, 2006 11:08 AM
> > > > 
> > > > 
> > > > Hello,
> > > > I have a slot in my class which expects integers as parameter
> > > > and
> > > would
> > > > like to connect this to a button. What should happen on clicking
> > > > the
> > > > button is that the text of the button should be read and
> > > > converted to
> > > an
> > > > integer and then it should be sent to the slot. E.g.:
> > > > 
> > > > 
> > > > void FrameWork::test(int) {...}
> > > > 
> > > > 
> > > > int tmp = left_click->text().toInt();
> > > > connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));
> > > > 
> > > > 
> > > > Unfortunately, this does not work because of the type-safety of
> > > signals
> > > > and slots such that I could only write something like
> > > > 
> > > > 
> > > > connect(button01,SIGNAL(clicked()),this,SLOT(test()));
> > > > 
> > > > 
> > > > but here I would miss the needed parameter. Is there a way of
> > > connecting
> > > > the two as described above or do i have to derive from
> > > > QPushButton and
> > > > overwrite the signal clicked() such that it sends its contents
> > > > as
> > > > integer?
> > > > 
> > > > 
> > > 
> > > 
> > > I don't know of a general solution to your problem, but
> > > considering this
> > > specific case, you could use sender() in your test slot.
> > > 
> > > 
> > > void FrameWork::test()
> > > {
> > >    //adding type safety recommended
> > >    int t =
> > > static_cast<QAbstractButton*>(sender())->text().toInt();
> > >    //...
> > > }
> > 
> > 
> > Thanks for the tip, but the slot is called by several different
> > objects,
> > such that I would have to check whether the sender really was a
> > QAbstractButton or whether it was a QComboBox. Is there a smart
> > (Qt-)way
> > of checking this? 
> > 
> > 
> > Thanks once more
> > Tim
> > 
> > 
> > 
> > 
> > --
> > 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/
> > 
> > 
> 
> -- 
> Brad Howes
> bradhowes@xxxxxxx
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> Brad Howes
> Group 42
> MIT Lincoln Laboratory â 244 Wood St. â Lexington, MA 02173
> Phone: 781.981.5292 â Fax: 781.981.3495 â Secretary: 781.981.7420
> 
> 
> 
> 
> 

--
 [ signature omitted ] 

Message 6 in thread

> > I don't know of a general solution to your problem, but considering this
> > specific case, you could use sender() in your test slot.
> > 
> > void FrameWork::test()
> > {
> >    //adding type safety recommended
> >    int t = static_cast<QAbstractButton*>(sender())->text().toInt();
> >    //...
> > }
> 
> Thanks for the tip, but the slot is called by several different objects,
> such that I would have to check whether the sender really was a
> QAbstractButton or whether it was a QComboBox. Is there a 
> smart (Qt-)way of checking this? 

if (QPushButton * pb = qobject_cast<QPushButton *>(sender()))
	// it's a QPushButton (-derived class)
else if (QComboBox * cb = qobject_cast<QComboBox *>(sender()))
	// ...

But QSignalMapper might indeed be the way to go...

Andre'

--
 [ signature omitted ] 

Message 7 in thread

>if (QPushButton * pb = qobject_cast<QPushButton *>(sender()))

Shouldn't this be :

QPushButton *pb = qobject_cast<QPushButton*>(sender());
if(pb) // it's a pushbutton

?


--
 [ signature omitted ] 

Message 8 in thread

Those statements are equivalent, the '=' operator returns the value that
was assigned, so after the whole cast and assignment takes place, the
value that remains inside of "if()" is the same value that was assigned
to "pb".

So the original statement works, it just is shorthand and could possibly
be confusing!

Sean

-----Original Message-----
From: Duane Hebert [mailto:spoo@xxxxxxxxx] 
Sent: Thursday, August 31, 2006 11:36 AM
To: qt-interest@xxxxxxxxxxxxx
Subject: Re: small signal/slot problem

>if (QPushButton * pb = qobject_cast<QPushButton *>(sender()))

Shouldn't this be :

QPushButton *pb = qobject_cast<QPushButton*>(sender());
if(pb) // it's a pushbutton

?


--
 [ signature omitted ] 

Message 9 in thread

>Those statements are equivalent, the '=' operator returns the value that
>was assigned, so after the whole cast and assignment takes place, the
>value that remains inside of "if()" is the same value that was assigned
>to "pb".

Right.

>So the original statement works, it just is shorthand and could possibly
>be confusing!

Apparently <g>


--
 [ signature omitted ] 

Message 10 in thread

Tim Kietzmann wrote:

> Thanks for the tip, but the slot is called by several different objects,
> such that I would have to check whether the sender really was a
> QAbstractButton or whether it was a QComboBox. Is there a smart (Qt-)way
> of checking this?

Call sender() in the slot to find out which object emitted the signal

  http://doc.trolltech.com/4.1/qobject.html#sender

then determine if the class inherits QAbstractButton or QComboBox with the
inherits() function:

  http://doc.trolltech.com/4.1/qobject.html#inherits

-- 
 [ signature omitted ] 

Message 11 in thread

Sounds like a QSignalMapper is right up your alley.

ry

> On Tue, 2006-08-29 at 11:23 -0700, Tom Matelich wrote:
> > > From: Tim Kietzmann [mailto:tkietzma@xxxxxx]
> > > Sent: Tuesday, August 29, 2006 11:08 AM
> > > 
> > > Hello,
> > > I have a slot in my class which expects integers as parameter and
> > would
> > > like to connect this to a button. What should happen on clicking the
> > > button is that the text of the button should be read and converted to
> > an
> > > integer and then it should be sent to the slot. E.g.:
> > > 
> > > void FrameWork::test(int) {...}
> > > 
> > > int tmp = left_click->text().toInt();
> > > connect(button01,SIGNAL(clicked()),this,SLOT(test(tmp)));
> > > 
> > > Unfortunately, this does not work because of the type-safety of
> > signals
> > > and slots such that I could only write something like
> > > 
> > > connect(button01,SIGNAL(clicked()),this,SLOT(test()));
> > > 
> > > but here I would miss the needed parameter. Is there a way of
> > connecting
> > > the two as described above or do i have to derive from QPushButton and
> > > overwrite the signal clicked() such that it sends its contents as
> > > integer?
> > > 
> > 
> > I don't know of a general solution to your problem, but considering this
> > specific case, you could use sender() in your test slot.
> > 
> > void FrameWork::test()
> > {
> >    //adding type safety recommended
> >    int t = static_cast<QAbstractButton*>(sender())->text().toInt();
> >    //...
> > }
> 
> Thanks for the tip, but the slot is called by several different objects,
> such that I would have to check whether the sender really was a
> QAbstractButton or whether it was a QComboBox. Is there a smart (Qt-)way
> of checking this? 
> 
> Thanks once more
> Tim
> 
> 
> --
> 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 ]