PDA

View Full Version : Program crashes when radioButton selected



ebsaith
12th June 2013, 11:47
when I click any of the radioButons displayed in my mainWindow... runtime error program crashes(pic att)

This is my connect statement in constructor of main class


connect(ui->radioButton_1, SIGNAL(clicked()), this, SLOT(getInfo()));
connect(ui->radioButton_2, SIGNAL(clicked()), this, SLOT(getInfo()));


Slot: getInfo
[code]
void Dialog::getInfo()
{
my_qlabel alpha; // create an instance of class my_qlabel to access public member variables

if(ui->radioButton_1->isChecked())
{
reCounter += 1;
reX[reCounter] = alpha.x1;
reY[reCounter] = alpha.y1;
reO[reCounter] = alpha.theta;
//ui->radioButton_1->setChecked(false);
}
else if ...
[code]

Not sure if the connect statement is causing the crash or the if statement

please help
Kind Regards

MSUdom5
12th June 2013, 16:18
Did you try running in a debugger and printing the stack trace after the crash? Should point you to the offending line...

jesse_mark
12th June 2013, 21:58
what is the size of reX , reY and reO ?? and what is the initial value of the reCounter

amleto
12th June 2013, 23:16
are your pointers initialised?

karankumar1609
13th June 2013, 07:18
Make sure that your GUI is initiated before connect statement.

Post your class code, there will be some hidden mistakes.

ebsaith
13th June 2013, 10:16
Done some modifying on code... to no avail though

Basically I need to check which radio button selected
The Problem -> RadioButton in main dialog Class(Window)
-> the Check statement is in a different class("label class" within Window/dialog)

Created signals & slots as:


Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
connect(ui->radioButton_1, SIGNAL(n1_Selected()), my_qlabel, SLOT(my_qlabel::HandleN1()));
connect(ui->radioButton_2, SIGNAL(n2_Selected()), my_qlabel, SLOT(my_qlabel::HandleN2()));
...
}


in dialog:


void Dialog::on_radioButton_1_clicked()
{
emit n1_Selected();
}

void Dialog::on_radioButton_2_clicked()
{
emit n2_Selected();
}


my slots in sub class (label class)
.h


private slots:
void HandleN1();
void HandleN2();

.cpp


void my_qlabel::HandleN1()
{
//calculations
}

void my_qlabel::HandleN2()
{
//diff-calculations
}


Above not working!
Where am I going wrong?
How do I fix it?

Thanks

karankumar1609
13th June 2013, 10:25
As i have seen some issues in your code implementation.



// Make sure my_qlabel is is initialized before you use.

my_qlabel = new QLabel(this); // my_qlabel is not initialized in your code

// ui->radioButton_1 should be 'this' because you are emitting a signal from dialog class
connect(ui->radioButton_1, SIGNAL(n1_Selected()), my_qlabel, SLOT(HandleN1()));

// ui->radioButton_2 should be 'this' because you are emitting a signal from dialog class
connect(ui->radioButton_2, SIGNAL(n2_Selected()), my_qlabel, SLOT(HandleN2()));



Try changing these things...

CHEERS..!!

ebsaith
13th June 2013, 10:55
Thanks, simple as that :)

d_stranz
16th June 2013, 04:28
No, not as simple as that. Do you want to create a *new* QLabel every time a radio button is clicked? That is what is now happening if you modified your code to follow the previous answer. Those new QLabel instances have no relationship at all to any QLabel that might be in your GUI they are simply dangling my_qlabel pointers that will eventually get deleted when the main window goes out of scope.