Skip to content
  • 0 Votes
    10 Posts
    7k Views
    C

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

  • 0 Votes
    1 Posts
    665 Views
    No one has replied