PDA

View Full Version : SIGNAL and SLOT problem



giblit
26th March 2013, 23:45
I'm trying to keep track of which button(sender) is being clicked I tried using the QObject::sender() but that gives me weird numbers(ex: 0xb07f970) and not like an index
so then I tired this for my header:


class: QPushButton;
class: QGridLayout;
private slots:
void timeButton_pressed(QPushButton *timeButton[24]);

private:
QPushButton *timeButton[24];
QGridLayout *layout;


and on my c++


class::class(){
layout = new QGridLayout;

for(int i = 0; i < 24; i++){
timeButton[i] = new QPushButton;
QString inum = QString("%1").arg(i);
timeButton[i]->setText(inum);
timeButtons->addButton(timeButton[i],i);
textEdit[i] = new QTextEdit;

layout->addWidget(timeButton[i]);
connect(timeButton[i],SIGNAL(clicked()),
this,SLOT(timeButton_pressed(QPushButton*)));
}
setLayout(layout);
}

void class::timeButton_pressed(QPushButton* timeButton[24]){
cout << "SIGNAL BEING SENT" << endl;
for(int i = 0;i<24;i++){
QString inum = QString("%1").arg(i);
if(inum == timeButton[i]->text()){
cout << "button: " << i << " has been pressed!" << endl;
}
}
//cout << "BUTTON " << QObject::sender() << " has been pressed" << endl;
}

but the signal doesn't even seem to be sent to the timeButton_Pressed
and if I remove the (QPushButton*) from connection and the functions it works but...if I press one button it thinks I pressed all the buttons because I'll get a cout of

button: 0 has been pressed!
button: 1 has been pressed!
button: 2 has been pressed!
button: 3 has been pressed!
button: 4 has been pressed!
button: 5 has been pressed!
button: 6 has been pressed!
button: 7 has been pressed!
button: 8 has been pressed!
button: 9 has been pressed!
button: 10 has been pressed!
button: 11 has been pressed!
button: 12 has been pressed!
button: 13 has been pressed!
button: 14 has been pressed!
button: 15 has been pressed!
button: 16 has been pressed!
button: 17 has been pressed!
button: 18 has been pressed!
button: 19 has been pressed!
button: 20 has been pressed!
button: 21 has been pressed!
button: 22 has been pressed!
button: 23 has been pressed!
but if I pressed button 0 it should only of couted "button: 0 has been pressed!" and not all the others.

I can think of one way to fix this...but I would rather not do it because of time.
one fix would be to just connect each signal to a different slot and then make 24 slots and each slot do something different.

can anyone think of a way for me to have them all on one slot but do different things based on the button being pressed?

Thanks for any suggestions!

Added after 8 minutes:

sorry guys, was missing one tiny line in the slot =/
if anyone else has this problem add in this line


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

then just do an if statement

if(button == buttonname){
//blah
}

wysota
27th March 2013, 07:24
Take a look at the QSignalMapper class.

Lesiok
27th March 2013, 07:41
QObject::sender() gives You a pointer to the object so slot should looks like this :

void class::timeButton_pressed(void)
{
cout << "SIGNAL BEING SENT" << endl;
for(int i = 0;i<24;i++)
{
if(QObject::sender() == timeButton[i])
{
cout << "button: " << i << " has been pressed!" << endl;
break;
}
}
}

d_stranz
27th March 2013, 20:12
but...if I press one button it thinks I pressed all the buttons

No it doesn't. The loop is doing exactly what you told it to:

For each integer between 0 and 24
{
Make a QString containing the integer
Fetch the "timeButton" from the array at the given integer location
Fetch the text from the button
Compare the text and the QString, and if they are equal, print a phrase.
}

That loop doesn't care at all which button was pressed. All it is doing is comparing strings which were assigned in the constructor.

You can either do as Lesiok suggested, and examine QObject::sender(), or you can implement a QSignalMapper as Wysota suggests. Either way will get you what you want.

amleto
27th March 2013, 21:37
connect(timeButton[i],SIGNAL(clicked()), this,SLOT(timeButton_pressed(QPushButton*)));

this will never work. you cannot connect a signal to a slot where the slot has more arguments

giblit
27th March 2013, 22:16
Yeah you can amleto you can have as many signals as you want for a slot.
I figured it out though yesterday by using a cast but lesiok's post does exact same thing took me a while to figure out I was missing the == button[i] after checking the sender
thanks again

anda_skoa
28th March 2013, 21:08
My recommendation for dealing with multiple, potentially dynamically created buttons, is QButtonGroup

Cheers,
_