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

QPushButton that run external app



  • Hi,

    I have a quick question. I created a QPushButton called extapp_btn.
    Now I want to connect it with an external app. In my example bellow
    I push the button and notepad.exe should start.

    How can I connect them?

    -example code ----------------------------------

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWebView>
    #include <QUrl>
    #include <QPushButton>
    #include <QProcess>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    ui->webView->load(QUrl("http://localhost"));

    QPushButton * extapp_btn = new QPushButton(this);
    extapp_btn->setGeometry(540,440,93,27);
    extapp_btn->setText("External App");
    

    QProcess *proc = new QProcess (this);
    // proc->start ("notepad.exe");

    QObject::connect(extapp_btn_btn,SIGNAL(clicked()),qApp,SLOT( START EXTERNAL APP ));

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }


  • Lifetime Qt Champion

    Well just put the QProcess code into
    START EXTERNAL APP slot ?
    Or do u ask how to make slot ?
    Also you say that qApp has the slot.
    Dont you mean "this" for mainwidow ?



  • Thanks for helping me out. I am complete noob. So what I want to do is. If I click on the button „extapp_btn“ notpad should start. How do I have to modify my code?


  • Lifetime Qt Champion

    @AZimmerer

    You should read this
    http://doc.qt.io/qt-5/signalsandslots.html

    Anyway, you need to add slot to mainwin.

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    ....
    private slots: // this you might to write too
    void StartApp(); // this is the slot. a normal function
    ..

    and in mainwindow.cpp
    void MainWindow::StartApp() {
    QProcess::startDetached("notepad.exe");
    }

    then
    QObject::connect(extapp_btn_btn,SIGNAL(clicked()),this,SLOT( StartApp()));



  • thank you very much. worked like a charm :D



  • Hi,

    You can even give the file to be opened in arguments :

    QProcess::startDetached("notepad++",  QStringList() << "path/to/the/file.txt");
    


  • @Gojir4 said in QPushButton that run external app:

    Hi,

    You can even give the file to be opened in arguments :

    QProcess::startDetached("notepad++",  QStringList() << "path/to/the/file.txt");
    

    Thanks for the help. much appreciated


Log in to reply