maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ? (https://talk.maemo.org/showthread.php?t=57821)

gri 2010-07-05 19:55

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

((MainWindow*)parent())->someFunctionDefinedInMainWindow();
Please never ever do something like this! If you want to use your class inside Canvas, make a property. Never expect canvas to be always a child of your MainWindow.
Secondly, you're writing c++ code, not c - so use the c++ casts or your Qt favourite cast like:
Code:

MainWindow* myWindow = qobject_cast<MainWindow*>(someObject);
Code:

void Canvas::refreshFoldersList(QString inputPath)
{
    QDir *dir = new QDir(inputPath);
    dir->setFilter(QDir::AllDirs);
    dir->setSorting(QDir::Name);

Better make this a stack variable, you're leaking memory without any sense.

Please be also aware of passing your this pointer from your class' constructor. This may lead to problems when using derived classes. (The this pointer points to a memory fragment of the class whose constructor you are in. This means in your MainWindow::MainWindow(), this points to the vtable of MainWindow. In QMainWindow::QMainWindow(), this points to the vtable of QMainWindow. In every other function except constructors and destructors the this pointer always points to the vtable of your derived class - MainWindow in your case.)

Joorin 2010-07-05 22:04

Re: [Qt] Functions cannot access objects defined in the same class .. ! Possibly a bug ?
 
And yet again the intricacies of C++ are there to trip you.


All times are GMT. The time now is 11:10.

vBulletin® Version 3.8.8