![]() |
[Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
This first issue has been solved (my silly mistake!):
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: Code:
class Canvas : public QWidget//, public MainWindow 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 ? Second problem is still an ongoing issue: 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: Code:
((MainWindow*)parent())->someFunctionDefinedInMainWindow(); Code:
void * papa; Code:
canvas->papa = this; //this being the MainWindow instance initializing canvas Code:
((MainWindow*)papa)->someFunctionDefinedInMainWindow(); But again, I would like to know the nature of these problems .. Am I doing something wrong or what ? |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
How about changing all the "-->" to "->"? I don't think there is a operator -->
|
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
Quote:
|
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
Scope is a semantic property, getting a segmentation fault when accessing an attribute is something else.
Please supply the code where you "initialize foldersList in the browseFolders() function". Most likely you're not allocating memory in the way you're expecting to. Also, make sure you're using the same object instance when initializing and referencing. |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
Alright, here's the refreshFoldersList() function
Code:
void Canvas::refreshFoldersList(QString inputPath) Code:
void Canvas::browseFolders() |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
You define an attribute named foldersList and then you're surprised that things go wrong when you do
Code:
QListWidget *foldersList = new QListWidget(); foldersList will get a new value every time and it will be a local variable and not the object attribute that gets updated. |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
Quote:
|
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
problem: QListWidget *foldersList = new QListWidget();
you define new QListWidget pointer in browseFolders() and it cant be seen outside correct: foldersList = new QListWidget(); |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
wow, lol .. am I embarassed or what !? :o .. I guess I didn't get enough sleep last night :p
Thanks for pointing out my silly mistake .. But the second issue I mentioned in OP still is a mystery for me .. Although I've found a workaround but still, any insight into that would be really helpful too .. |
Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
Again: Talking about code is pretty useless in this context. Please supply the code where everything you describe is done.
Saying that you set something as the parent of something else is good information but code is king. |
All times are GMT. The time now is 22:30. |
vBulletin® Version 3.8.8