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
}
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
}