PDA

View Full Version : variable in method not initialized?!



frosch
2nd September 2006, 12:38
hello,
i have a question. i have build kind of a qt
widget, when the widget is constructed the
"createWidget" method is called. in this
method also an attribute of the class is
initialized (the attribute *table).

everything works fine - the widget is displayed.
but when i called a method, which accesses the
attribute "table" (e.g. the method "insertName")
a error appears: the pointer is empty?!

damn, why isn't the pointer stored in the variable
"table"?!

greeting and thanks



void PortCheckboxTable::createWidget(QWidget* parent)
{
HxPort::createWidget(parent);

table = new QTable(1, 1, baseWidget);
table->setEnabled(sensitivity);
table->verticalHeader()->hide();
table->setLeftMargin(0);
table->setSorting(false);
table->setDragEnabled(false);
table->setSelectionMode(QTable::NoSelection);
table->setHScrollBarMode(QScrollView::AlwaysOff);
table->horizontalHeader()->hide();
table->setTopMargin(0);
table->setColumnStretchable(0, true);

initTable();

//for(int i = 0; i < 3; i++ )
// table->setItem( i, 0, new QCheckTableItem( table, "Check me" ) );
layout->addWidget(table);
layout->activate();
baseWidget->adjustSize();
}

void PortCheckboxTable::insertName(const char* name)
{
if( tableWithMessage ){
//table->clearCellWidget(0, 0);
//table->setItem(0, 0, new QCheckTableItem(table, name));
table->numRows();
tableWithMessage = 0;
}
else
{
int newNumRows = table->numRows() + 1;
table->setNumRows(newNumRows);
table->setItem( newNumRows - 1, 0, new QCheckTableItem(table, QString(name)));
}
}

munna
2nd September 2006, 12:54
Where does the error appears ? Does your program crashes when u call insertName ?

wysota
2nd September 2006, 13:44
Do you actually call this createWidget() method from within the constructor? Maybe some other constructor is called which doesn't call createWidget().

frosch
2nd September 2006, 20:14
thanks for your replies. :)

@munna
yes, the program crashes, when i call the "insertName" method. until that
all works fine - the table is displayed on the gui.

it crashes at line 31 (see first post).



void PortCheckboxTable::insertName(const char* name)
{
(...)
table->numRows(); <- HERE IT CRASHES, BECAUSE TABLE == NULL
(...)
}


@wysota
my constructor looks like that:


PortCheckboxTable::PortCheckboxTable(HxObject *obj, const char* name) :
HxPort(obj,name)
{
table = NULL;
sensitivity = 1;
}

i don't call the method "createWidget", it is called indirectly - i think - from another
qt-object?! i have debugged the code: the "createWidget" method is called and the "table"
variable is initialized (table != NULL). but it seems, that the program forgets the table
pointer??? suddenly after the constructer is involved i call "insertName" and then a
NULL pointer exception appears. :crying:

@all
i'm not so familar with qt, when does qt call the "createWidget" method? is there another
way to setup the gui elements? if i insert the code lines of "createWidget" in my
constructor, the table isn't shown on my gui.

jacek
2nd September 2006, 20:27
i have debugged the code: the "createWidget" method is called and the "table"
variable is initialized (table != NULL).
Maybe it's being invoked on a different object?


i'm not so familar with qt, when does qt call the "createWidget" method?
is there another way to setup the gui elements? if i insert the code lines of "createWidget" in my
constructor, the table isn't shown on my gui.
Where did you find that createWidget() method? You can create widgets wherever you want, but you have to remember to invoke show() if you create widgets after their parent was shown.

What is that baseWidget which you use as a parent?

wysota
2nd September 2006, 22:27
i don't call the method "createWidget", it is called indirectly - i think - from another qt-object?
I have never heard of such a method in Qt public API. Where did you get it from? I suggest you move the contents of createWidget() to the constructor.

frosch
2nd September 2006, 23:42
to build my own gui elements in my application, i have to inherit from the class "HxPort"
and overwrite the virtual function "createWidget".



class PortCheckboxTable : public HxPort {
(...)
PortCheckboxTable(HxObject* object, const char* name);
(...)
protected:
QTable *table;
(...)
virtual void guiCallback();
virtual void createWidget(QWidget* parent);
(...)
}

jacek
2nd September 2006, 23:45
What is that HxPort class? Does it invoke createWidget() automatically? If yes, when?

frosch
3rd September 2006, 09:00
the "HxPort" is a section on a gui, which can be filled with customized gui elements.
it calls the "createWidget" method once automatically, when the gui becomes visible.

wysota
3rd September 2006, 09:56
Maybe you should call it sooner -- from within the constructor?

jacek
3rd September 2006, 14:09
it calls the "createWidget" method once automatically, when the gui becomes visible.
It might be called too late. Try to invoke show() on all widgets you create in createWidget() and make sure that insertName() is invoked later.


Maybe you should call it sooner -- from within the constructor?
It won't work --- it's a virtual method.