PDA

View Full Version : Accessing child widgets Qtable cause segmentation fault?



yektaayduk
16th January 2008, 17:23
Hi

Inside a child widget (=checklists) I have created a QTable . I want to access QTable Items of this table by calling a child widget function ,Report_Test1() .

When I call Report_Test1() from the main Widget(=GLObjectWindow ) the line related to QtableItem access cause a segmentation fault.

The simplified source code for main.cpp ,globjwin.cpp.h,selftest.cpp.h are below.

Can you contribute to solution,thank you.


int main( int argc, char **argv )
{

GLObjectWindow w;
a.setMainWidget( &w );

}

//--------globjwin.h--------------------
class GLObjectWindow : public QWidget
{
Q_OBJECT
public:
GLObjectWindow( QWidget *parent=0, const char *name=0 );
public:
CheckLists *checklists;
}
//----------globjwin.h------------------


//----------globjwin.cpp----------------
GLObjectWindow::GLObjectWindow( QWidget* parent, const char* name )
: QWidget( parent, name )
{
}

void GLObjectWindow::CreateSelfTestDialog( )
{
printf("CreateSelfTestDialog executed\n");

static CheckLists *checklists=new CheckLists;
checklists->show();
checklists-> raise();
checklists->setWindowState(Qt::WindowActive);
}

void GLObjectWindow::GetTestReports( )
{
checklists->Report_Test1();//++++++++++++++++++++++++++++++++--calling from here cause segfault
checklists->Report_Test2();//++++++++++++++++++++++++++++++++--calling from here cause segfault
}
//----------globjwin.cpp----------------



//----selftest.h------------------------

class CheckLists : public QWidget
{
Q_OBJECT

public:
CheckLists( QWidget *parent = 0, const char *name = 0 );

public:
void Report_Test1();
void Report_Test2();


public:

QTable *table_selftest;
QHeader *header;

};
//----selftest.h------------------------


//------------selftest.cpp--------------

CheckLists::CheckLists( QWidget *parent, const char *name )
: QWidget( parent, name )
{


QHBoxLayout *lay = new QHBoxLayout( this );
lay->setMargin( 5 );

// create a widget which layouts its childs in a column
QVBoxLayout *vbox1 = new QVBoxLayout( lay );
vbox1->setMargin( 5 );

// First child: a Label
vbox1->addWidget( new QLabel( "Check some items!", this ) );


table_selftest = new QTable( 5,2,this );
vbox1->addWidget( table_selftest );


table_selftest->horizontalHeader()->setLabel( 0, trUtf8(" Test") );
table_selftest->horizontalHeader()->setLabel( 1, trUtf8(" Result") );

QTableItem *test_1 = new QTableItem( table_selftest, QTableItem::Never, trUtf8( "Test 1") );
table_selftest->setItem( 0, 0, test_1 );

QTableItem *test_2 = new QTableItem( table_selftest, QTableItem::Never, trUtf8( "Test 2 ") );
table_selftest->setItem( 1, 0, test_2 );


QTableItem *test_3 = new QTableItem( table_selftest, QTableItem::Never, trUtf8( "Test 3") );
table_selftest->setItem( 2, 0, test_3 );


table_selftest->setText( 0, 1, "-" );
table_selftest->setText( 1, 1, "-" );
table_selftest->setText( 2, 1, "-" );

Report_Test1(); //-++++++++++++++++++++++-calling from here doesnt cause segfault
Report_Test2();//--+++++++++++++++++++++++calling from here doesnt cause segfault

}
//------------------------------------------------------------------------------------
void CheckLists::Report_Test1()
{

this->table_selftest->setText( 1, 1, "OK" );
}
//------------------------------------------------------------------------------------
void CheckLists::Report_Test2()
{

this->table_selftest->setText( 2, 1, "OK" );
}
//------------selftest.cpp--------------

Yekta Ayduk

wysota
16th January 2008, 17:26
static CheckLists *checklists=new CheckLists;
This is a static variable local to the method, not the checklists variable declared in the GLObjectWindow class. As you assign the object to the static field, you leave the other one as a dangling pointer.

yektaayduk
16th January 2008, 18:55
I have corrected the line

static CheckLists *checklists=new CheckLists;

as

checklists=new CheckLists;

The segmentation fault disappeared.I made a google search ,the article below has the same opinion as wysota.

Statics: Schizophrenia for C++ Programmers
The confusion over statics comes from its multiple interpretations. When you see the keyword static, it usually takes on one of two interpretations. It may indicate that the variable is ``allocated once at a fixed location'' as opposed to being allocated on the stack. The other interpretation indicates that the variable or function is ``local to a particular scope'' and is not visible everywhere throughout the program like a global variable might be. ...



Yekta Ayduk