Results 1 to 3 of 3

Thread: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?

  1. #1
    Join Date
    Jun 2010
    Posts
    26
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?

    Hey guys .. Well ever since I've started programming in Qt, I've been having problems regarding scope visibility of objects I've defined .. Till now I've managed to find ways to get around these things but now its getting annoying ..

    For example, I have this class defined called Canvas:

    Qt Code:
    1. class Canvas : public QWidget//, public MainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. void refreshFoldersList(QString inputPath);
    6. void browseFolders();
    7.  
    8. private:
    9. QListWidget *foldersList;
    10.  
    11. };
    12.  
    13. #endif // CANVAS_H
    To copy to clipboard, switch view to plain text mode 


    Now even though foldersList is private, refreshFoldersList() should be able to 'see' it, right ? But in my case it can't .. ! I first initialize foldersList in the browseFolders() function and then from within browseFolders(), I call refreshFoldersList() ... Any code inside refreshFoldersList() dealing with foldersList immediately throws a segmentation fault ... I've checked the pointer values for foldersList when the scope is in browseFolders() and refreshFoldersList() .. the values don't match .. its like either I'm trying to access something I'm not supposed to, or that I'm trying to access an object which has not been initialized yet ..

    Any clues on this ?

    A related issue ... I have another class MainWindow (inherited from QMainWindow) .. In this class I have an instance of the class Canvas .. this instance is named canvas .. I initialize canvas in MainWindow's constructor, and set canvas's parent to the MainWindow instance initializing it .. based on this, I used the following code to access a MainWindow function from within the Canvas class:

    Qt Code:
    1. ((MainWindow*)parent())->someFunctionDefinedInMainWindow();
    To copy to clipboard, switch view to plain text mode 

    Before, the above code used to work .. but then like 2-3 days ago it stopped working all of a sudden .. Now it got me to inside the MainWindow function I was calling (namely someFunctionDefinedInMainWindow() ), but from there if I tried to access any variable defined in MainWindow, I got a Segmentation Fault, again with pointer values not matching .. The way I solved this is by defining a variable as:

    Qt Code:
    1. void * papa;
    To copy to clipboard, switch view to plain text mode 

    .. inside Canvas, and then when I initialized canvas, I set:

    Qt Code:
    1. canvas->papa = this; //this being the MainWindow instance initializing canvas
    To copy to clipboard, switch view to plain text mode 

    Now I accessed MainWindow's functions like this:

    Qt Code:
    1. ((MainWindow*)papa)->someFunctionDefinedInMainWindow();
    To copy to clipboard, switch view to plain text mode 

    .. which works!

    But again, I would like to know the nature of these problems .. Am I doing something wrong or what ?
    Last edited by ahmadka; 5th July 2010 at 14:37.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bu

    This class definition looks ok, but there are a few things wrong
    Qt Code:
    1. class Canvas : public QWidget//, public MainWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. void refreshFoldersList(QString inputPath);
    6. void browseFolders();
    7.  
    8. private:
    9. QListWidget *foldersList;
    10.  
    11. };
    To copy to clipboard, switch view to plain text mode 

    Note that from within refreshFoldersList and browseFolders, the foldersList is visible and usable as long as you stay in the scope of the class. That's ok.

    But, you do not define a constructor and destructor.
    Furthermore, QWidget really likes a parent, without it, it doesn't know what to do when everyone else gets destroyed.
    Thus: pass the parent to the subclass.
    Even more: set a parent of the foldersList or make sure you destroy it in the destructor. If you set a parent, the foldersList gets destroyed for you when the canvas class gets destroyed.

    Another thing I would personally improve is make sure all my pointers are created in the constructor. Try to do this as early as possible for class wide pointers. It's really annoying having public functions that rely on other public functions to be called first.

    Now this:
    Qt Code:
    1. ((MainWindow*)parent())->someFunctionDefinedInMainWindow();
    To copy to clipboard, switch view to plain text mode 
    Please don't do this.
    1. Use signals and slots for communication between objects
    2. Or use events

    If you use a signal to tell the main window something is done, you can then connect a slot to it and call a function in your canvas object. This way you don't have to pass a pointer of the main window to your objects.
    Yes, the parent is the main window. Don't use it from within your objects.



    ps: to give a good answer to why you get crashes, please post your whole code.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bu

    Quote Originally Posted by ahmadka View Post
    Now even though foldersList is private, refreshFoldersList() should be able to 'see' it, right ? But in my case it can't .. ! I first initialize foldersList in the browseFolders() function and then from within browseFolders(), I call refreshFoldersList() ... Any code inside refreshFoldersList() dealing with foldersList immediately throws a segmentation fault ... I've checked the pointer values for foldersList when the scope is in browseFolders() and refreshFoldersList() .. the values don't match .. its like either I'm trying to access something I'm not supposed to, or that I'm trying to access an object which has not been initialized yet ..

    Any clues on this ?
    You have answered your own question. Your private member variable is a pointer to a QListWidget. What is not at all evident is anywhere you actually create an instance of the QListWidget for that pointer to be pointing at i.e.
    Qt Code:
    1. foldersList = new QListWidget(this);
    To copy to clipboard, switch view to plain text mode 
    is missing. So, in all likelihood, you are using foldersList without it having been initialised. Tbscope has some good suggestions in that regard.

Similar Threads

  1. class calling other class functions?
    By Hardstyle in forum Newbie
    Replies: 4
    Last Post: 2nd June 2010, 02:38
  2. Replies: 4
    Last Post: 29th May 2010, 12:56
  3. Replies: 4
    Last Post: 26th June 2007, 19:19
  4. How to access MainWindow objects.
    By Doug Broadwell in forum Newbie
    Replies: 1
    Last Post: 3rd November 2006, 10:03
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

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.