PDA

View Full Version : Dynamically linking signals and slots for buttons



chandru@080
6th December 2010, 12:37
I have created some dynamic buttons through XML in my code. I want to know how do i connect slots to this dynamically created buttons?

high_flyer
6th December 2010, 15:33
Here you go:
http://doc.trolltech.com/4.7/signalsandslots.html

chandru@080
7th December 2010, 10:00
Thanks for the response.. i dint frame my question well.. i have some more queries on the same subject
I have created 5 buttons from XML. and that is displayed on the screen. i want to know how to do the following things

1. If i click on dynamically created button how can i identify which button i have clicked?
2. How do i get the button name when i click on the button?
3. How do i set an event for the button

Any respone will be of great help

FelixB
7th December 2010, 11:00
inside a slot, you can get the sender by "QPushButton* button = qobject_cast<QPushButton*>sender()". If the cast fails, button is NULL.

Please show us the code how you create the buttons.

chandru@080
7th December 2010, 12:14
I find the number of child nodes and then get the count using the QDomNodeList and then use a for loop connect the signal with the slots like this,



connect(Button1[i],SIGNAL(clicked()),this,SLOT(Display()));


I need to know which is the button that is clicked in the Display button and there is where i am stuck

This is how i create my buttons.


Button1[i] = new QPushButton( this);
Button1[i]->setGeometry(QRect(x ,y,button_width,button_height));
Button1[i]->setText(QApplication::translate("Widget", text.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
Button1[i]->show();

high_flyer
7th December 2010, 12:19
I need to know which is the button that is clicked in the Display button and there is where i am stuck

FlixB already answered that - you can use the sender() method to compare against your buttons.

chandru@080
7th December 2010, 12:37
Wiseguy.. its true that FixB told me how to go about it.. but i am completely new to QT and i am doing and things are going over my head. so its taking me time to figure out how to use the sender function and other API's

FelixB
7th December 2010, 12:56
Wiseguy.. its true that FixB told me how to go about it.. but i am completely new to QT and i am doing and things are going over my head. so its taking me time to figure out how to use the sender function and other API's

it's not a problem being a beginner. i started with qt some months ago and a lot of things were confusing to me. BUT: what is your problem now? put the line i posted above in your "Display()" method and you have access to the clicked button.

this thread is something like:
A: can someone help me adding 2 and 3?
B: use "plus"-operator
A: thank you, but i need to know how to add the values
C: use the solution posted by A
B: sorry, I'm new. I don't know how to use "plus"

chandru@080
7th December 2010, 13:05
Well Said... There is no problem now.. I got it done. thanks a lot. I completely agree with the steps that you mentioned.. i was going round and round and later found that it was such a simple thing that can be done in no time... well i guess i need to bang my head somewhere for wasting so much of time.

Felix thanks a ton for the response.

minirop
7th December 2010, 18:59
to identify a button, use the QSignalMapper.
short example :


QSignalMapper * mapper = new QSignalMapper(this);
QDomElement child = elem.firstChildElement("choice");
while(!child.isNull())
{
QPushButton * button = new QPushButton(child.text());
connect(button, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(button, child.attribute("value"));
vLayout->addWidget(button);

child = child.nextSiblingElement("choice");
}
connect(mapper, SIGNAL(mapped(const QString &)), this, SLOT(onbuttonClicked(const QString &)));
the slot :

void Mainwindow::onbuttonClicked(const QString& value)
{
// value is here to identify the button
}
And I use it with this kind of XML :

<choices name="image">
<choice value="animated">Show an animated image</choice>
<choice value="not">Show a picture</choice>
<choice value="nothing">Pass</choice>
</choices>

so when I click on "Show a picture", the slot "onbuttonClicked" is called with the value "not".

pradhan
21st November 2013, 07:04
Hi ,

As felix said i have used "QPushButton* button = qobject_cast<QPushButton*>sender()" in my push button click slot. But when i compile i get the following error:

error: cannot resolve overloaded function 'qobject_cast' based on conversion to type 'QPushButton*'

Kindly help.

high_flyer
21st November 2013, 09:53
It should be:


QPushButton* button = qobject_cast<QPushButton*>(sender());

gab74
7th April 2014, 11:46
I try to use the code below :



while(!child.isNull())
{
QPushButton * button = new QPushButton(child.text());
connect(button, SIGNAL(clicked()), &mapper, SLOT(map()));
mapper.setMapping(button, child.attribute("value"));
vLayout->addWidget(button);

child = child.nextSiblingElement("choice");
}
connect(mapper, SIGNAL(mapped(const QString &)), this, SLOT(onbuttonClicked(const QString &)));



in the SLOT I receive correctly the value passed



void Mainwindow::onbuttonClicked(const QString& value)
{
// value is here to identify the button

QPushButton* button = qobject_cast<QPushButton*>(sender());

}


the button object is always NULL, why this ?
I want to retrieve button name but the sender is always NULL..

anda_skoa
7th April 2014, 13:03
the button object is always NULL, why this ?

The sender of the signal is the QSIgnalMapper. It cannot be casted to QPushButton, so the qobject_cast returns a null pointer.



I want to retrieve button name but the sender is always NULL..

If you mean button pointer, see QSignalMapper::mapping()

Cheers,
_