How to restart an application by pressing a button?



  • Hello all,
    As I described on the header, I want the application restarted (or rebooted I can say) when a button is clicked.
    Example button:

    Rectangle
                        {
                            id:helloButton
                            width:80
                            height:80
                            color: "transparent"
                            Image{
                                id:hellobg
                                source:"qrc:/hellobg.png"
                                sourceSize.height: 80
                                height: 80
                                fillMode: Image.PreserveAspectFit
                                antialiasing: true
                                smooth: true
                                opacity:1
                                    MouseArea{
                                    anchors.fill: parent
                                    onClicked: {
                                        hello3bg.opacity = 0.2
                                        hello2bg.opacity = 0.2
                                        hellobg.opacity = 1
    
                                   //////////  SOME CODE HERE TO RESTART
                                   //////////  THE APPLICATION FOR ME
    
    
                                        }
                                    }
                                }
                        }
    


  • @closx hi
    qApp->quit();
    QProcess::startDetached(qApp->arguments()[0], qApp->arguments());

    but this is c++, so expose it to your qml if you need



  • @LeLev Hi and thanks for your answer!
    Actually I did not know that I am not able to do it on QML. So I should look around for how can I expose a C++ function into QML right? I also want to change opacities of some buttons if some conditions exits, or some signals are triggered. I was trying to do something like that,

    Image{
                                id:chinese
                                source:"qrc:/design/settings/chinese.png"
                                sourceSize.height: 80
                                height:80
                                fillMode: Image.PreserveAspectFit
                                antialiasing: true
                                smooth: true
                                opacity: 0.2
                                if(SM.Lang == MyLang.CH)
                                {
                                opacity:1
                                }
                                MouseArea{
                                anchors.fill: parent
                                onClicked: {
                                    SM.lang = MyLang.CH
                                    chinese.opacity = 1
                                    turkish.opacity = 0.2
                                    english.opacity = 0.2
                                    }
                                }
                                }
    

    But it gave error on line I wrote the 'if' statement,

    Unexpected token 'if'.
    Expected a qualified name id.
    

  • Moderators

    hi @closx replace

    opacity: 0.2
    if(SM.Lang == MyLang.CH)
    {
    opacity:1
    }

    with

    opacity: SM.Lang == MyLang.CH ? 1 : 0.2
    


  • @J.Hilk Hey, Thanks for your answer!
    Your patch "kind of" solved the problem. Error was gone, but opacities won't change by language on restart. When I kept trying, I saw that problem is deeper. Probably something about my initial settings and translator defaults.
    Thanks anyway!



  • So everyone, I create a new header and source named "Restarter".
    restarter.h

    #ifndef RESTARTER_H
    #define RESTARTER_H
    
    #include <QObject>
    
    
    class Restarter : public QObject
    {
        Q_OBJECT
    
    public:
        explicit Restarter(QObject *parent = nullptr);
        Q_INVOKABLE void makeRestart();
    
    signals:
    
    public slots:
    };
    
    #endif // RESTARTER_H
    
    

    restarter.cpp (wrote a test function)

    #include "restarter.h"
    #include <QMessageBox>
    
    Restarter::Restarter(QObject *parent) :
        QObject (parent)
    {
    }
    
    void Restarter::makeRestart()
    {
        QMessageBox msg;
        msg.setText("Hello world");
        msg.exec();
    }
    
    

    in main.cpp somewhere

    #include "restarter.h"
        qmlRegisterType<Restarter>("closx.restarter", 1, 0, "Restarter");
    

    and in my qml file somewhere

    import closx.restarter 1.0
    Restarter {
            id:restarter
        }
    Image{
                                id:turkish
                                source:"qrc:/design/settings/turkish.png"
                                sourceSize.height: 80
                                height: 80
                                fillMode: Image.PreserveAspectFit
                                antialiasing: true
                                smooth: true
                                opacity:0.2
                                    MouseArea{
                                    anchors.fill: parent
                                    onClicked: {
                                        SM.lang = MyLang.TR
                                        chinese.opacity = 0.2
                                        english.opacity = 0.2
                                        turkish.opacity = 1
                                        restarter.makeRestart
                                        }
                                    }
                                }
    

    Message box wont appear. What am I doing wrong?


  • Moderators

    @closx said in How to restart an application by pressing a button?:

    restarter.makeRestart

    makeRestart is a function - you should get a warning in your console output regarding this

    restarter.makeRestart()
    


  • @J.Hilk OH! How can I be that stupid :D Thanks for the answer! Function is working now.
    Btw, when I try to execute a QMessageBox in a QML app, the app is crashed...


  • Moderators

    @closx
    creating Ui components based on QWidgets from QML is not really supported (the other way around it is)

    You probably have the wrong QApplication type, missing modules in the pro file or something along that line.

    I would suggest sticking to QML-ui if your App is based around that.
    You can take a look at KDAB's declarative-widgets if your interested in it



  • @J.Hilk Hey, Thank you for all of your help. Solved the problem thanks to you!

    If there is anyone facing the same issue, solved like this,
    Created a source file named "restarter"
    restarter.h

    #ifndef RESTARTER_H
    #define RESTARTER_H
    
    #include <QObject>
    
    
    class Restarter : public QObject
    {
        Q_OBJECT
    
    public:
        explicit Restarter(QObject *parent = nullptr);
        Q_INVOKABLE void makeRestart();
    
    signals:
    
    public slots:
    };
    
    #endif // RESTARTER_H
    
    

    restarter.cpp (note: my application works as a service named "myservice" on system, so I can not restart it as restarting a regular application.)

    #include "restarter.h"
    #include <QProcess>
    
    
    Restarter::Restarter(QObject *parent) :
        QObject (parent)
    {
    }
    
    void Restarter::makeRestart()
    {
        QProcess::execute("sudo service myservice restart");
    }
    
    

    If your application works NOT AS A SERVICE, BUT AN APPLICATION,
    restarter.cpp

    #include "restarter.h"
    #include <QApplication>
    #include <QProcess>
    
    
    Restarter::Restarter(QObject *parent) :
        QObject (parent)
    {
    }
    
    void Restarter::makeRestart()
    {
    qApp->quit();
     QProcess::startDetached(qApp->arguments()[0], qApp->arguments()); //application restart
    }
    
    

    You need to register "restarter.cpp" as a QML registery (I know, stupid sentence)
    So you need to insert these lines in main.cpp

    #include "restarter.h"
    qmlRegisterType<Restarter>("closx.restarter", 1, 0, "Restarter");
    

    and use it on your QML file:

    import closx.restarter 1.0
    Restarter {
            id:restarter
        }
    
                               Rectangle{
                                   id: restartbg
                                   width: 120
                                   height: 70
                                   radius: 8
                                   color:"black"
                                   anchors.centerIn: parent
                                   z:344
                                   Text{
                                       anchors.centerIn: parent
                                       text:qsTr("Restart") + mytrans.emptyString
                                       font.family:GSystem.myriadproita.name
                                       font.pixelSize: 18
                                       color: "white"
                                   }
                                   MouseArea{
                                       anchors.fill: parent
                                       onClicked: {
                                           restarter.makeRestart()
                                       }
                                       onPressed: {
                                           restartbg.color = "#1c1c1c"
                                       }
                                       onReleased: {
                                           restartbg.color = "black"
                                       }
                                   }
                               }
    

    Again, thanks to @LeLev and @J-Hilk for everything.