Results 1 to 11 of 11

Thread: variable in method not initialized?!

  1. #1
    Join Date
    Sep 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default variable in method not initialized?!

    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

    Qt Code:
    1. void PortCheckboxTable::createWidget(QWidget* parent)
    2. {
    3. HxPort::createWidget(parent);
    4.  
    5. table = new QTable(1, 1, baseWidget);
    6. table->setEnabled(sensitivity);
    7. table->verticalHeader()->hide();
    8. table->setLeftMargin(0);
    9. table->setSorting(false);
    10. table->setDragEnabled(false);
    11. table->setSelectionMode(QTable::NoSelection);
    12. table->setHScrollBarMode(QScrollView::AlwaysOff);
    13. table->horizontalHeader()->hide();
    14. table->setTopMargin(0);
    15. table->setColumnStretchable(0, true);
    16.  
    17. initTable();
    18.  
    19. //for(int i = 0; i < 3; i++ )
    20. // table->setItem( i, 0, new QCheckTableItem( table, "Check me" ) );
    21. layout->addWidget(table);
    22. layout->activate();
    23. baseWidget->adjustSize();
    24. }
    25.  
    26. void PortCheckboxTable::insertName(const char* name)
    27. {
    28. if( tableWithMessage ){
    29. //table->clearCellWidget(0, 0);
    30. //table->setItem(0, 0, new QCheckTableItem(table, name));
    31. table->numRows();
    32. tableWithMessage = 0;
    33. }
    34. else
    35. {
    36. int newNumRows = table->numRows() + 1;
    37. table->setNumRows(newNumRows);
    38. table->setItem( newNumRows - 1, 0, new QCheckTableItem(table, QString(name)));
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: variable in method not initialized?!

    Where does the error appears ? Does your program crashes when u call insertName ?
    Last edited by munna; 2nd September 2006 at 13:02.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: variable in method not initialized?!

    Do you actually call this createWidget() method from within the constructor? Maybe some other constructor is called which doesn't call createWidget().

  4. #4
    Join Date
    Sep 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: variable in method not initialized?!

    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).

    Qt Code:
    1. void PortCheckboxTable::insertName(const char* name)
    2. {
    3. (...)
    4. table->numRows(); <- HERE IT CRASHES, BECAUSE TABLE == NULL
    5. (...)
    6. }
    To copy to clipboard, switch view to plain text mode 

    @wysota
    my constructor looks like that:
    Qt Code:
    1. PortCheckboxTable::PortCheckboxTable(HxObject *obj, const char* name) :
    2. HxPort(obj,name)
    3. {
    4. table = NULL;
    5. sensitivity = 1;
    6. }
    To copy to clipboard, switch view to plain text mode 
    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.

    @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.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: variable in method not initialized?!

    Quote Originally Posted by frosch
    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?

    Quote Originally Posted by frosch
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: variable in method not initialized?!

    Quote Originally Posted by frosch
    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.

  7. #7
    Join Date
    Sep 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: variable in method not initialized?!

    to build my own gui elements in my application, i have to inherit from the class "HxPort"
    and overwrite the virtual function "createWidget".

    Qt Code:
    1. class PortCheckboxTable : public HxPort {
    2. (...)
    3. PortCheckboxTable(HxObject* object, const char* name);
    4. (...)
    5. protected:
    6. QTable *table;
    7. (...)
    8. virtual void guiCallback();
    9. virtual void createWidget(QWidget* parent);
    10. (...)
    11. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: variable in method not initialized?!

    What is that HxPort class? Does it invoke createWidget() automatically? If yes, when?

  9. #9
    Join Date
    Sep 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: variable in method not initialized?!

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: variable in method not initialized?!

    Maybe you should call it sooner -- from within the constructor?

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: variable in method not initialized?!

    Quote Originally Posted by frosch
    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.

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

Similar Threads

  1. custom plugin designer property with out a variable?
    By high_flyer in forum Qt Programming
    Replies: 1
    Last Post: 15th March 2006, 19:11

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.