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?
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?
Here you go:
http://doc.trolltech.com/4.7/signalsandslots.html
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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
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.
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,
Qt Code:
connect(Button1[i],SIGNAL(clicked()),this,SLOT(Display()));To copy to clipboard, switch view to plain text mode
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.
Qt Code:
Button1[i]->setText(QApplication::translate("Widget", text.toStdString().c_str(), 0, QApplication::UnicodeUTF8)); Button1[i]->show();To copy to clipboard, switch view to plain text mode
FlixB already answered that - you can use the sender() method to compare against your buttons.I need to know which is the button that is clicked in the Display button and there is where i am stuck
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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)
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.
to identify a button, use the QSignalMapper.
short example :
the slot :Qt Code:
while(!child.isNull()) { connect(button, SIGNAL(clicked()), &mapper, SLOT(map())); mapper.setMapping(button, child.attribute("value")); vLayout->addWidget(button); child = child.nextSiblingElement("choice"); }To copy to clipboard, switch view to plain text mode
And I use it with this kind of XML :Qt Code:
void Mainwindow::onbuttonClicked(const QString& value) { // value is here to identify the button }To copy to clipboard, switch view to plain text mode
Qt Code:
<choices name="image"> <choice value="animated">Show an animated image</choice> <choice value="not">Show a picture</choice> <choice value="nothing">Pass</choice> </choices>To copy to clipboard, switch view to plain text mode
so when I click on "Show a picture", the slot "onbuttonClicked" is called with the value "not".
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.
It should be:
Qt Code:
To copy to clipboard, switch view to plain text mode
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
I try to use the code below :
Qt Code:
while(!child.isNull()) { connect(button, SIGNAL(clicked()), &mapper, SLOT(map())); mapper.setMapping(button, child.attribute("value")); vLayout->addWidget(button); child = child.nextSiblingElement("choice"); }To copy to clipboard, switch view to plain text mode
in the SLOT I receive correctly the value passed
Qt Code:
void Mainwindow::onbuttonClicked(const QString& value) { // value is here to identify the button }To copy to clipboard, switch view to plain text mode
the button object is always NULL, why this ?
I want to retrieve button name but the sender is always NULL..
Last edited by anda_skoa; 7th April 2014 at 12:00. Reason: changed [qtclass] to [code]
Bookmarks