PDA

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



ahmadka
5th July 2010, 14:06
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 ?

tbscope
5th July 2010, 14:50
This class definition looks ok, but there are a few things wrong

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

private:
QListWidget *foldersList;

};

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:

((MainWindow*)parent())->someFunctionDefinedInMainWindow();
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.

ChrisW67
7th July 2010, 00:14
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.
foldersList = new QListWidget(this); is missing. So, in all likelihood, you are using foldersList without it having been initialised. Tbscope has some good suggestions in that regard.