Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Code to launching a program



  • I want to know how to make a pushbutton that when clicked will open a program or file(such as a text file or internet explorer). I do not need to read or write to it, just launch/open it.



  • Ok...First you need to create a SLOT and connect to QPushButton, this SLOT is called when a SIGNAL clicked() is emitted by QPushButton. A tip, "SIGNAL/SLOT":http://qt-project.org/doc/qt-5/signalsandslots.html is key of Qt, I suggest you read more about this.

    And to open a file or program you can use "QProcess":http://qt-project.org/doc/qt-5/QProcess.html.

    In practice it would be something like this:

    @

    QPushButton *button = new QPushButton("Button", this);
    connect(button, SIGNAL(clicked()), this, SLOT(openProgramFile()))

    @

    So you create a SLOT....

    Add this in your Header file (.h):

    @

    protected slot:

    void openProgramFile();

    @

    Add this in your Source file (.cpp):

    @

    void yourClass::openProgramFile(){

    QString programPath = "/path/to/program";
    QStringList arguments;
    arguments << "argument1" << "argument2";
    
    QProcess *myProcess = new QProcess(this&#41;;
    myProcess->start(programPath, arguments);
    

    }

    @



  • Place a QPushButton, use QObject::connect() to connect it to a method of your choosing, inside that method use QProcess to launch a different program. Quite easily done, actually!

    EDIT: I didn't notice that somebody else already wrote a much more detailed reply, hadn't refreshed the page after a little bit of waiting.



  • Thanks for the help!

    I also have tried the system(" file directory "); and that works to.

    I would like to have the program be hidden and close after certain time. QProsses be able to do this to?


  • Qt Champions 2017

    QProcess will help you to kill/terminate created process. You will not be able to hide it through QProcess


Log in to reply