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



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



  • 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
              }
        }
    }
    


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



  • 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



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



  • 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
        }
    
    }
    
    


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



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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.