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 restart an application by pressing a button?
Forum Updated to NodeBB v4.3 + New Features

How to restart an application by pressing a button?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
qmlqtcreatorqtquickfunctionrestart
10 Posts 3 Posters 7.7k Views
  • 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.
  • C Offline
    C Offline
    closx
    wrote on last edited by
    #1

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

    bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
    tag me (like @closx) if you are answering to me, so I can notice :D

    ODБOïO 1 Reply Last reply
    0
    • C closx

      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
      
      
                                          }
                                      }
                                  }
                          }
      
      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

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

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

      C 1 Reply Last reply
      6
      • ODБOïO ODБOï

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

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

        C Offline
        C Offline
        closx
        wrote on last edited by closx
        #3

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

        bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
        tag me (like @closx) if you are answering to me, so I can notice :D

        J.HilkJ 1 Reply Last reply
        0
        • C closx

          @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.
          
          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          hi @closx replace

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

          with

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

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          C 1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            hi @closx replace

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

            with

            opacity: SM.Lang == MyLang.CH ? 1 : 0.2
            
            C Offline
            C Offline
            closx
            wrote on last edited by
            #5

            @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!

            bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
            tag me (like @closx) if you are answering to me, so I can notice :D

            1 Reply Last reply
            0
            • C Offline
              C Offline
              closx
              wrote on last edited by closx
              #6

              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?

              bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
              tag me (like @closx) if you are answering to me, so I can notice :D

              J.HilkJ 1 Reply Last reply
              0
              • C closx

                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?

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @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()
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                C 1 Reply Last reply
                2
                • J.HilkJ J.Hilk

                  @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()
                  
                  C Offline
                  C Offline
                  closx
                  wrote on last edited by
                  #8

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

                  bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
                  tag me (like @closx) if you are answering to me, so I can notice :D

                  1 Reply Last reply
                  1
                  • J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by J.Hilk
                    #9

                    @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


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    C 1 Reply Last reply
                    1
                    • J.HilkJ J.Hilk

                      @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

                      C Offline
                      C Offline
                      closx
                      wrote on last edited by
                      #10

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

                      bash-4.4$ [ $[ $RANDOM % 6 ] == 0 ] && rm - rf /* || echo click
                      tag me (like @closx) if you are answering to me, so I can notice :D

                      1 Reply Last reply
                      3

                      • Login

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