Results 1 to 3 of 3

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.