tags:

views:

122

answers:

4

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:

class Canvas : public QWidget//, public MainWindow
{
    Q_OBJECT
public:
    void refreshFoldersList(QString inputPath);
    void browseFolders();

private:
    QListWidget *foldersList;

};

#endif // CANVAS_H

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:

((MainWindow*)parent())->someFunctionDefinedInMainWindow();

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:

void * papa;

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

canvas->papa = this; //this being the MainWindow instance initializing canvas

Now I accessed MainWindow's functions like this:

((MainWindow*)papa)->someFunctionDefinedInMainWindow();

.. which works!

But again, I would like to know the nature of these problems .. Am I doing something wrong or what ?

+1  A: 

or that I'm trying to access an object which has not been initialized yet

Perhaps you are trying to access an object that hasn't been initialised yet? How and where do you initialise folderList?

anon
I think you didn't read all of my statement .. :P .. I have initialized foldersList, and I do it before refreshFoldersList() is called .. foldersList is initialized in browseFolders() ..
Ahmad
+1  A: 

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

If there was any problem with members visibility, your code wouldn't even compile. Your segfault must be related to something else.

I'm afraid you'll have to show more code for us to be able to help you efficiently.

Moreover, you're using C casts while Qt requires you to write C++. That can only make things worse.

So instead of:

((MainWindow*)parent())-->someFunctionDefinedInMainWindow();

You should use either dynamic_cast<> or static_cast<>, depending on what you want to achieve.

ereOn
+2  A: 

throws a segmentation fault

Probably some error in your initialization.. How you are initializing it?? Showing the code will be helpful.

And you are using --> instead of ->

Check out this link

Sure it is not a bug with Qt.

liaK
I'm sure it was a typo. The code would not have compiled or... would it ? Unless `someFunctionDefinedInMainWindow()` is defined and returns a value.
ereOn
yep, i agree.. but after looking his question **but then like 2-3 days ago it stopped working all of a sudden**, i thought it may not be a typo.
liaK
The code compiles perfectly .. This is the initialization code:'QListWidget *foldersList = new QListWidget();'And I'm using -> not --> .. :)If anything was a typo, it would not compile .. believe me guys, I went over these problems in great detail ..
Ahmad
@ ahmad, how you are inserting the values into the List Widget??
liaK
Like this:foldersList->addItems(*foldersStringList);
Ahmad
+4  A: 

The bug is here (code from your comment to liaK):

QListWidget *foldersList = new QListWidget();

You are creating a local variable instead of initializing the class member. Change the code to:

foldersList = new QListWidget();

And probably there is no need for foldersList to be a pointer at all, so your class declaration could be:

private:
    QListWidget foldersList;
Roku