Solved QButtonGroup->button() - app crashes
-
Hi folks!
I'm trying to get the reference of a QAbstractButton which is held by a QButtonGroup.
According to the Qt documentation, I need to use QButtonGroup::button(id).That's my code:
sP::sP(QWidget *parent) : QWidget(parent), ui(new Ui::sP) { ui->setupUi(this); QButtonGroup *keypad = new QButtonGroup(); keypad->addButton(ui->key_1, 1); keypad->addButton(ui->key_s1, 2); connect(keypad, SIGNAL(buttonClicked(int)), this, SLOT(keypadClicked(int))); } void sP::keypadClicked(int ButtonID) { keypad->button(ButtonID); }
At executing keypadClicked(), my app just crashes with the classical windows error: ... is not responding/has stopped working.
The connection works without any issues (e.g. if I'm executing
ui->label->setText(QString::number(ButtonID));
for keypadClicked(), then it writes correctly the ID of the pressed button into the label).I really don't get what is wrong with my function. (it's of course not the final keypadClicked()-function, but I want to keep it as simple as possible for you ;))
Thanks!
-
keypad in constructor it's local object.
in keypadClicked slot keypad not exist.
Use in slot:QButtonGroup *keypad = dynamic_cast<QButtonGroup*>(sender()); keypad->button(ButtonID); ...
-
@t0m4 said in QButtonGroup->button() - app crashes:
sP::sP(QWidget *parent) :
QWidget(parent),
ui(new Ui::sP)
{
ui->setupUi(this);QButtonGroup *keypad = new QButtonGroup(); keypad->addButton(ui->key_1, 1); keypad->addButton(ui->key_s1, 2); connect(keypad, SIGNAL(buttonClicked(int)), this, SLOT(keypadClicked(int)));
}
void sP::keypadClicked(int ButtonID) {
keypad->button(ButtonID);
}
Whereever that
keypad
member object comes from, inside keypadClicked, it's not the one you create in the constructor. -
Hey guys, thanks for your very quick answers!
Makes totally sense, the scopes are obviously different...well, if I don't get a compile error but a windows error, that's really not the first thing I'm looking for :-)
Since I'm declaring QButtonGroup keypad;* in my header, my own solution would've been to not declare it again in my .cpp-file (code above) but just go with keypad = new QButtonGroup();.
Merry Christmas!