Hi,

2013/12/12  <christopher.l...@thurweb.ch>:
> Thanks, that works! (except that you probably did not mean "view" to be 2nd
> param of setContextProperty).
>
> So taking your example I can do:
>
> int main(int argc, char *argv[])
> {
>     [...]
>     QGuiApplication *app = SailfishApp::application(argc, argv);
>     QQuickView *view = SailfishApp::createView();
>     [...]
>     app->exec();
> }

You probably want to write "return app->exec();" at the end, so that
your main() function returns a value (and using the app->exec() result
for that is a good idea).

> But combining your approach, and the QScopedPointers, this also works:
>
>
> int main(int argc, char *argv[])
> {
>     [...]
>     QScopedPointer<QGuiApplication> app(SailfishApp::application(argc,
> argv));
>     QScopedPointer<QQuickView> view(SailfishApp::createView());
>     [...]
>     app->exec();
> }
>
> So which of the 2 approaches is better, and why?

As described on the QScopedPointer documentation page:
http://qt-project.org/doc/qt-5.0/qtcore/qscopedpointer.html

What QScopedPointer does in practice is when it falls out of scope (at
the end of main() in this example), it will delete the object it
points to, so in that case, what it will do conceptually is the same
as if you were to write at the of main() before returning:

delete view;
delete app;

This frees the memory of both objects and makes sure that the
destructors are called and things are cleaned up.

As your process is probably going away as soon as your main() function
ends, this is not so problematic (the memory allocated to that process
is freed then the process is closed), but it might make a difference
if you depend on the destructor of both objects being called. So you
could avoid the QScopedPointer usage and still get the same behavior
when you do it like that (again, modifying your examples so that main
returns the return value of app->exec()):

int main(...)
{
    QGuiApplication *app = ...;
    QQuickView *view = ...;
    ...
    int result = app->exec();
    delete view;
    delete app;
    return result;
}

In general, not using QScopedPointer should be fine (and the
application might "save" some time by not tearing down the view), but
the most correct way would be to explicitly delete the view and app or
use QScopedPointer, which adds a bit of convenience at the expense of
being a bit puzzling at first.

For additional geek points and confusion, consider the following:

typedef QScopedPointer<QGuiApplication> OGuiApplication;
typedef QScopedPointer<QQuickView> OQuickView;

int main(...)
{
    OGuiApplication app = ...;
    OQuickView view = ...;
    ...
    return app->exec();
}


HTH :)
Thomas

> Zitat von "Wim de Vries" <wsvr...@xs4all.nl>:
>
>
>> Hi Cristopher,
>> I got this answer some time ago:
>>
>>     QGuiApplication *app = SailfishApp::application(argc, argv);
>>     QQuickView *view = SailfishApp::createView();
>>     view->rootContext()->setContextProperty("cppproperty", view);
>> view->setSource(SailfishApp::pathTo("qml/main.qml"));
>>     view->showFullScreen();
>>     app->exec();
>>
>> r
>> wim
>>
>
>
> _______________________________________________
> SailfishOS.org Devel mailing list
_______________________________________________
SailfishOS.org Devel mailing list

Reply via email to