PDA

View Full Version : How can I know which button click.



electronicboy
3rd October 2009, 13:58
sory for my bad english. I have two buttons. And two buttons are connecting to slot for click signal. But slots are equal.

An Example;


connect(bottun1,SIGNAL(clicked()),this,SLOT(MyClic kEvent()));
connect(button2,SIGNAL(clicked()),this,SLOT(MyClic kEvent()));

How can I know which button click?

kwisp
3rd October 2009, 14:51
see
QObject::sender()

electronicboy
3rd October 2009, 17:27
@kwisp thanks. I looked sender but i didn t find properties. I want to ObjectName and I try

sender()->ObjectName() but it's null.

Edit: I solve this problem I write

button1->setObjectName("button1");

and running now.

I have an array and I want to create buttons for each member of this array. can you help me?

kwisp
3rd October 2009, 19:17
...
I have an array and I want to create buttons for each member of this array. can you help me?
I do not understand you.
Can you show me your code.?

drhex
3rd October 2009, 19:39
With QSignalMapper you can have your slot called with an (e.g. integer) argument that is different for each button.

electronicboy
3rd October 2009, 21:11
Hi,

I have programme in C#.Net. But I want to write application for embedded operation system and i use QT.

in C#.net code


//Button Arraylist
string[] buttonsarray = new string[2] { "firstbutton", "secondbutton"};


Dictionary<string, Button> buttonlist = new Dictionary<string, Button>();
private void createButton()
{
//each index of this array
foreach (string name in buttonsarray)
{
this.buttonlist[name] = new Button();
this.buttonlist[name].Text = name;
this.buttonlist[name].Location = new System.Drawing.Point(16*(countx+1) + padding * countx, 10);
this.buttonlist[name].Size = new System.Drawing.Size(95, 145);
// Show form
this.Controls.Add(buttonlist[name]);
countx += 1;
}
}

drhex
4th October 2009, 08:35
...and your question is ... ?

electronicboy
4th October 2009, 11:40
How can I create buttons from arraylist?

soxs060389
4th October 2009, 12:11
use a signal with an int parameter:


class YourButton {
public:
// stuff here
private:
int id;
signals:
void buttonPressed(int);
};

and now just give any button an ID at creation. and send that within the signal so you can distinguish between buttons (and of course the slot should have an int argument aswell)


so whenever a button is pressed (detect that via the QPushButton signals ( bind a slot with that and then emit your own signal)).


emit buttonPressed(id);



What kind of data does your array / list hold?

electronicboy
4th October 2009, 12:24
use a signal with an int parameter:


class YourButton {
public:
// stuff here
private:
int id;
signals:
void buttonPressed(int);
};

and now just give any button an ID at creation. and send that within the signal so you can distinguish between buttons (and of course the slot should have an int argument aswell)


so whenever a button is pressed (detect that via the QPushButton signals ( bind a slot with that and then emit your own signal)).


emit buttonPressed(id);



What kind of data does your array / list hold?

List hold is button name.

electronicboy
4th October 2009, 14:27
Hi all.

I could not tell my trouble. I have an arraylist. For example;



QList<QPushButton*> buttonlist;
buttonlist << button1<<button2<<button3;


This arraylist is changeable as you know. And Qt want to definition for QPushButton(button1,button2,button3) . How can I define this QPushBotton. I hope I can tell my problem.