Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to avoid leaking memory?
Forum Updated to NodeBB v4.3 + New Features

How to avoid leaking memory?

Scheduled Pinned Locked Moved QML and Qt Quick
11 Posts 4 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vsorokin
      wrote on last edited by
      #2

      Can you give more details, what do you mean?

      --
      Vasiliy

      1 Reply Last reply
      0
      • ZlatomirZ Offline
        ZlatomirZ Offline
        Zlatomir
        wrote on last edited by
        #3

        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.

        https://forum.qt.io/category/41/romanian

        1 Reply Last reply
        0
        • S Offline
          S Offline
          spode
          wrote on last edited by
          #4

          @

          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());
          
          schermataParola->setSource(QUrl::fromLocalFile("qml/HeilfeFuerVieleWoerterZuErinnern/schermataParola.qml"));
          schermataParola->show();
          

          }
          @

          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++.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on last edited by
            #5

            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":http://doc.qt.nokia.com/latest/gettingstartedqml.html and "Closing a Qt Quick application from QML":http://www.developer.nokia.com/Community/Wiki/CS001626_-_Closing_a_Qt_Quick_application_from_QML.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              spode
              wrote on last edited by
              #6

              so, how do you use Qt::WA_deleteonclose attribute?
              @
              QDeclarativeView *view = new QDeclarativeView;
              view->setAttribute(Qt::WA_DeleteOnClose);
              view->rootContext()->setContextProperty("parolemodello", QVariant::fromValue(this->makeListaParolaSalvate(this->getCurrentFilter())));
              view->setSource(QUrl::fromLocalFile("qml/HeilfeFuerVieleWoerterZuErinnern/main.qml"));
              view->show();

              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...

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lgeyer
                wrote on last edited by
                #7

                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":http://www.developer.nokia.com/Community/Wiki/CS001626_-_Closing_a_Qt_Quick_application_from_QML link I've posted?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  spode
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lgeyer
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      spode
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lgeyer
                        wrote on last edited by
                        #11

                        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
                          {
                          public:
                          gestore()
                          {
                          view = new QDeclarativeView;
                          ...
                          qApp->setProperty("gestore", QVariant::fromValue<gestore*>(this));
                          }
                          QDeclarativeView getView() const { return view; }

                        private:
                        QDeclarativeView* view;
                        }
                        Q_DECLARE_METATYPE(gestore*);
                        ...
                        qApp->property("gestore").value<gestore*>()->getView()->close();
                        @
                        Brain to terminal. Not tested. Exemplary.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved