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 we can reset main.qml and automate it start when i clicked on text type in qml

how we can reset main.qml and automate it start when i clicked on text type in qml

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 2 Posters 3.3k Views 2 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    hi,we have a main.qml,i designed a text type in it,that has a text value:"reset me"
    how we can reset main.qml and automate it start when i clicked on text type in main.qml

    Text{
    text:"reset me";
    MouseArea{onClicked:{
    //here whats method we use
    }
    
    }
    }
    

    whats method should i use??thanks for reply.

    1 Reply Last reply
    0
    • CharbyC Offline
      CharbyC Offline
      Charby
      wrote on last edited by
      #2

      Could you please provide more explanations about what would you like to do when the text is clicked ?

      By design, MouseArea areas are not binded with the parent area so in your sample code, don't forget to define the geometry of the MouseArea :

      Text{
          text:"reset me";
          MouseArea{
               anchors.fill: parent
               onClicked:{
                //do whatever you like
                }
          }
      }
      
      stackprogramerS 1 Reply Last reply
      0
      • CharbyC Charby

        Could you please provide more explanations about what would you like to do when the text is clicked ?

        By design, MouseArea areas are not binded with the parent area so in your sample code, don't forget to define the geometry of the MouseArea :

        Text{
            text:"reset me";
            MouseArea{
                 anchors.fill: parent
                 onClicked:{
                  //do whatever you like
                  }
            }
        }
        
        stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by stackprogramer
        #3

        @Charby said:

        Could you please provide more explanations about what would you like to do when the text is clicked ?

        By design, MouseArea areas are not binded with the parent area so in your sample code, don't forget to define the geometry of the MouseArea :

        Text{
            text:"reset me";
            MouseArea{
                 anchors.fill: parent
                 onClicked:{
                  //do whatever you like
                  }
            }
        }
        

        but i know these rules,when i clicked on the text i want to file qml that is running now ,is stoped and exited and automate it is start my former qml file again.

        1 Reply Last reply
        0
        • CharbyC Offline
          CharbyC Offline
          Charby
          wrote on last edited by
          #4

          Actually, it depends of the purpose and the creation method of the Qml engine :

          • if you use a QQMLApplication you should close it, clear the components cache and call load again
          • If you are using a QuickView, you first clear the components cache and call setSource again
          • if you want to reload from QML, which is the most likely your goal, the easiest is to use a Loader element to (re)load your QML - thus the engine or view won't be reseted...

          You can find here a nice presentation of the 2 first option

          stackprogramerS 1 Reply Last reply
          0
          • CharbyC Charby

            Actually, it depends of the purpose and the creation method of the Qml engine :

            • if you use a QQMLApplication you should close it, clear the components cache and call load again
            • If you are using a QuickView, you first clear the components cache and call setSource again
            • if you want to reload from QML, which is the most likely your goal, the easiest is to use a Loader element to (re)load your QML - thus the engine or view won't be reseted...

            You can find here a nice presentation of the 2 first option

            stackprogramerS Offline
            stackprogramerS Offline
            stackprogramer
            wrote on last edited by stackprogramer
            #5

            @Charby said:

            • if you want to reload from QML, which is the most likely your goal, the easiest is to use a Loader element to (re)load your QML - thus the engine or view won't be reseted...

            thanks for reply
            when i reloaded the file.qml again,the file qml former is open and we have 2 file.qml
            i want to the former qml file is closed before the new file.qml start.

            CharbyC 1 Reply Last reply
            0
            • CharbyC Offline
              CharbyC Offline
              Charby
              wrote on last edited by
              #6

              Here is a short example showing method 1 and 3.

              QReseter.h

              #ifndef QRESETER_H
              #define QRESETER_H
              #include <QObject>
              #include <QQmlApplicationEngine>
              
              class QReseter : public QObject
              {
                  Q_OBJECT
              public:
                  explicit QReseter(QQmlApplicationEngine* engine);
                  virtual ~QReseter();
                  Q_INVOKABLE void reset();
              private:
                  QQmlApplicationEngine* m_pEngine = nullptr;
              };
              #endif // QRESETER_H
              

              qreset.cpp

              #include "qreseter.h"
              
              QReseter::QReseter(QQmlApplicationEngine* engine)
              {
                  m_pEngine = engine;
              
              }
              
              QReseter::~QReseter()
              {
              
              }
              
              void QReseter::reset()
              {
                  if (m_pEngine){
              
                      m_pEngine->clearComponentCache();
                      m_pEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
                  }
              }
              

              main.cpp

              #include <QGuiApplication>
              #include <QQmlApplicationEngine>
              #include <QQmlContext>
              #include "qreseter.h"
              
              int main(int argc, char *argv[])
              {
                  QGuiApplication app(argc, argv);
              
                  QQmlApplicationEngine engine;
                  QReseter reseter(&engine);
                  engine.rootContext()->setContextProperty("reseter", &reseter);
                  engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
              
                  return app.exec();
              }
              

              and finally the QML, main.qml

              import QtQuick 2.4
              import QtQuick.Window 2.2
              
              
              Window {
                  id:root
                  visible: true
                  width:640
                  height:480
              
                  Component{
                      id:qmlToLoad
                      Column{
                          anchors.fill : parent
              
                          Rectangle{
                              width: 300; height : 100; color:"yellow"
                              Text{
                                  anchors.centerIn: parent
                                  text:"reload QML - " + new Date().toTimeString()
                              }
                              MouseArea{
                                  anchors.fill: parent
                                  onClicked: {
                                      console.log("reload")
                                      loader.active = false;
                                      loader.active = true;
              
                                  }
                              }
                          }
                          Rectangle{
                              width: 300; height : 100;color:"orange"
                              Text{
                                  anchors.centerIn: parent
                                  text:"reset engine- " + new Date().toTimeString()
              
                              }
                              MouseArea{
                                  anchors.fill: parent
                                  onClicked: {
                                      root.close();
                                      reseter.reset();
              
                                  }
                              }
                          }
              
                      }
              
                  }
              
                  Loader{
                      id:loader
                      anchors.fill: parent
                      sourceComponent: qmlToLoad
                  }
              
              }
              
              
              1 Reply Last reply
              1
              • stackprogramerS stackprogramer

                @Charby said:

                • if you want to reload from QML, which is the most likely your goal, the easiest is to use a Loader element to (re)load your QML - thus the engine or view won't be reseted...

                thanks for reply
                when i reloaded the file.qml again,the file qml former is open and we have 2 file.qml
                i want to the former qml file is closed before the new file.qml start.

                CharbyC Offline
                CharbyC Offline
                Charby
                wrote on last edited by
                #7

                @stackprogramer

                Yes, you need to first close the window before calling load again as shown in my example below...
                As you can see, using the loader is the more straightforward option, furthermore it can ease the storing of states data outside of the loaded element.

                1 Reply Last reply
                0
                • stackprogramerS Offline
                  stackprogramerS Offline
                  stackprogramer
                  wrote on last edited by stackprogramer
                  #8

                  now i am sleepy ,tomorrow i will test it and i post the result here.thanks

                  1 Reply Last reply
                  0
                  • stackprogramerS Offline
                    stackprogramerS Offline
                    stackprogramer
                    wrote on last edited by stackprogramer
                    #9

                    now i tested it,my conclusion is sharing here,
                    my object is solving my problem in qml.so i use this method.
                    this some my source it is not all my source

                    Window{id:root
                    
                    
                    .
                    .
                    .
                    
                    roote.close();
                    pageLoader2.source = "main.qml"
                    
                    
                    }
                    

                    now first root object is closed and is loaded main.qml
                    @Charby my problem is solved.your qml code is used for me.
                    thanks very much

                    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