PDA

View Full Version : Segmentation fault error



Niamita
23rd April 2012, 08:57
Hi all

I am facing a error segmentation fault in the following code


mapper = new QSignalMapper ();

for(int i=0; i<obj_schedule_table->rowCount(); i++)
{
QWidget *widget1;
widget1 = obj_schedule_table->cellWidget(i,3);
QComboBox *combo = dynamic_cast<QComboBox*>(widget1->children().at(1));
combo->setEditable(false);

mapper->setMapping(combo, i);
connect(combo, SIGNAL(activated(QString)), mapper, SLOT (map()));
}
connect(mapper, SIGNAL(mapped(int)), this, SLOT(set_item_combobox_type(int)));

I know it may be due to my code is pointing some null pointer but i am not understanding what is the cause of error.

Thanks in advance.

Jonny174
23rd April 2012, 09:33
mapper = new QSignalMapper();

for (int i=0; i < obj_schedule_table->rowCount(); i++)
{
QWidget *widget1 = obj_schedule_table->cellWidget( i, 3 );
QComboBox *combo = dynamic_cast<QComboBox *>( widget1->children().at(1) );

if (combo)
{
combo->setEditable( false );
mapper->setMapping( combo, i );
connect(combo, SIGNAL(activated(QString)), mapper, SLOT (map()));
}
}

connect(mapper, SIGNAL(mapped(int)), this, SLOT(set_item_combobox_type(int)));


widget1->children().at(1)? Maybe widget1->children().count() < 1?

Niamita
23rd April 2012, 12:42
Does anybody help me what is wrong with above code.

wysota
23rd April 2012, 13:29
Either widget1 or widget1->children().at(1) is an invalid pointer.

nish
23rd April 2012, 13:36
did you tried to step debug your code?

d_stranz
23rd April 2012, 18:00
If you don't know how to use the debugger (and if you don't, how do you expect to become a good programmer?), modify your code to tell you what's wrong:



#include <QMessageBox>

mapper = new QSignalMapper ();

if ( obj_schedule_table != 0 )
{
for(int i=0; i<obj_schedule_table->rowCount(); i++)
{
QWidget * widget1 = obj_schedule_table->cellWidget(i,3);
if ( widget1 != 0 )
{
QComboBox * combo = dynamic_cast<QComboBox*>(widget1->children().at(1));
if ( combo != 0 )
{
combo->setEditable(false);

mapper->setMapping(combo, i);
connect(combo, SIGNAL(activated(QString)), mapper, SLOT (map()));
}
else
QMessageBox::critical( this, "Error", "Pointer to combo is NULL" );
}
else
QMessageBox::critical( this, "Error", "Pointer to widget1 is NULL" );
}
}
else
QMessageBox::critical( this, "Error", "Pointer to obj_schedule_table is NULL" );

connect(mapper, SIGNAL(mapped(int)), this, SLOT(set_item_combobox_type(int)));


Which message box do you see? That should give you a clue about where to look to solve the problem.