How to avoid leaking memory?

  • why do i am forced to use
    QDeclarativeView *schermataParola = new QDeclarativeView;

    to view a .qml file?
    there is no way to close the unused windows?

  • Can you give more details, what do you mean?

  • Doesn't your QDeclarativeView have a QWidget* parent? I mean the code posted is your actual code?
    Don't you have something like:
    @QDeclarativeView *schermataParola = new QDeclarativeView(someWidgetpointer);@
    or put the schermataParola into a layout?

    In case you do set a parent or it gets a parent (for example by putting schermataParola into a layout) the allocated memory will be deleted by the parent (when parent gets deleted or out of scope).

    If it doesn't get a parent you can set the Qt::WA_DeleteOnClose attribute or manually delete the pointer when the widget work is done.

  • @

    void gestore::visualizzaParola(int index)
    QDeclarativeView *schermataParola = new QDeclarativeView;
    biglietto bigliettocorrente = (biglietto) this->makeListaParolaSalvate(this->getCurrentFilter()).at(index);
    QDeclarativeContext *rootContext = schermataParola->rootContext();

    rootContext->setContextProperty("sfondoBiglietto", bigliettocorrente->gettipo());
    rootContext->setContextProperty("currentWort", bigliettocorrente->getwort());
    rootContext->setContextProperty("currentBeispiel", bigliettocorrente->getbeispiel());
    rootContext->setContextProperty("currentParola", bigliettocorrente->getparola());
    rootContext->setContextProperty("currentEsempio", bigliettocorrente->getesempio());


    you can see that it does not fall cause in that window there is nothing to close the window: there are just signals to open other windows with qt c++.

  • You will have to provide a possibility within your QML document to close it, usually a button or key event connected to Qt.quit(). Then the QDeclarativeEngine::quit() signal is emitted (accessible through QDeclarativeView::engine()), which has to be connected to a slot which handles the signal by for example deleting the view (QDeclarativeView::deleteLater()) or quitting the application.

    See "Getting Started Programming with QML": and "Closing a Qt Quick application from QML":

  • so, how do you use Qt::WA_deleteonclose attribute?
    QDeclarativeView *view = new QDeclarativeView;
    view->rootContext()->setContextProperty("parolemodello", QVariant::fromValue(this->makeListaParolaSalvate(this->getCurrentFilter())));

    QObject *obj = view->rootObject();
    QObject::connect(obj, SIGNAL(signal_creaNuovaParola()), this, SLOT(creaNuovaParola()));
    QObject::connect(obj, SIGNAL(signal_filterChanged(QString)), this, SLOT(visualizzaSchermataPrincipaleSlot(QString)));
    QObject::connect(obj, SIGNAL(signal_visualizzaParola(int)), this, SLOT(visualizzaParola(int)));


    so it does not function...

  • Of course it doesn't, because you have to have some piece of code in your QML which calls Qt.quit() and the quit() signal connected to some slot (preferably to close()) in C++. If you have the WA_DeleteOnClose flag set your view will get deleted too (as soon as the window is closed).

    Have you taken a look at the "Closing a Qt Quick application from QML": link I've posted?

  • yes, yes, i understand, but i need something that allows me to close a window as i open another. could please explain me how to realize that thing with QWigdet, please?

  • Just call view->close() when you open the other window.

  • right, but "view" is for me a local variable...

  • Make view a member variable then. If the other widget belongs to the same class it will be accessible directly, otherwise you will have to make it globally accessible, by

    • exposing it through a getter of your class and
    • making this class globally accessible
      class gestore
      view = new QDeclarativeView;
      qApp->setProperty("gestore", QVariant::fromValue<gestore*>(this));
      QDeclarativeView getView() const { return view; }

    QDeclarativeView* view;
    Brain to terminal. Not tested. Exemplary.

Log in to reply