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

QProcess and Slots



  • Hi, I'm very new to QT and trying to create a simple application that can trigger another application. I have tried several options and finally got to a point where it compiled without errors but it is not working:

    During run time I get this error: QObject::connect: No such slot QWidget::start_process("/home/qtsamples/sample3/./sample3")

    I'm trying to create a slot for QProcess but not getting the right syntax. Can anyone pls help me?

    #include <QApplication>
    #include <QWidget>
    #include <QPushButton>
    #include <QProcess>
    #include <QString>
    
    class MyButton : public QWidget {
        
     public:
         MyButton(QWidget *parent = 0);
    
     private slots:
         void start_process(QString process_name);
    
    };
    
    void MyButton::start_process (QString process_name){
      QProcess::execute(process_name);
    }
    
    MyButton::MyButton(QWidget *parent)
        : QWidget(parent) {
               
      QPushButton *quitBtn = new QPushButton("Quit", this);
      quitBtn->setGeometry(50, 40, 75, 30);
      
    
    
      connect(quitBtn, SIGNAL(clicked()), this,SLOT(start_process("/home/qtsamples/sample3/./sample3")));
    }
    
    int main(int argc, char *argv[]) {
        
      QApplication app(argc, argv);  
        
      MyButton window;
    
      window.resize(250, 150);  
      window.setWindowTitle("QPushButton");
      window.show();
    
      return app.exec();
    }
    

  • Lifetime Qt Champion

    Hi
    You cannot do
    connect(quitBtn, SIGNAL(clicked()), this,SLOT(start_process("/home/qtsamples/sample3/./sample3")));
    as the clicked() do not have any parameters and one cannot add
    extra in the connect statement. it must match.
    However you can do

    connect(quitBtn, &QPushButton::clicked, [](){
         start_process("/home/qtsamples/sample3/./sample3");
        });
    
    

    This is called a lambda.
    (its a in place slot )

    Note 2:
    You are not use QProcess correctly.
    One should not make one string with both path to exe and parameters.
    They should be seperate. That said, it might still work as long no spaces or
    anything else in path or parameter. However, i suggest to do like

     QString program = "./path/to/Qt/examples/widgets/analogclock";
     QStringList arguments;
     arguments << "-style" << "fusion";
     QProcess *myProcess = new QProcess(parent);
     myProcess->start(program, arguments);
    

  • Qt Champions 2017

    In addition to what @mrjj said, I prefer you to read how signal/slots work. How to pass arguments etc. Signal/Slots signature should match. In your case signal is not passing any value & slot is expecting the value.

    You can look at the example at my GIT



  • Thanks dheerendra & mrjj.
    Based on dheerendra comment, I understand that my use of slot & signals is not correct. I'm not sure how to modify it for this particular instance though. As per QT documentation, "Slots are normal C++ functions and can be called normally; their only special feature is that signals can be connected to them"

    So I'm not sure why my slot is not working. Anyway I have implemented this in the OnClicked function for the button directly and so don't need Signals/slots.


  • Lifetime Qt Champion

    @xXiXx said in QProcess and Slots:

    I'm not sure how to modify it for this particular instance though

    @mrjj already posted the code to do it properly.
    I would only fix one thing: add "this" to the capture list of the lambda:

    connect(quitBtn, &QPushButton::clicked, [this](){
         start_process("/home/qtsamples/sample3/./sample3");
        });
    

  • Lifetime Qt Champion

    @xXiXx if I see it correctly, your MyButton class is missing the Q_OBJECT macro. That is needed for signals&slots.


Log in to reply