开发者

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:

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 开发者_Go百科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 ?


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;


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.


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜