Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess and Slots
Forum Updated to NodeBB v4.3 + New Features

QProcess and Slots

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 1.2k Views 1 Watching
  • 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.
  • X Offline
    X Offline
    xXiXx
    wrote on last edited by xXiXx
    #1

    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();
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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);
      
      1 Reply Last reply
      6
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • X Offline
          X Offline
          xXiXx
          wrote on last edited by
          #4

          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.

          jsulmJ aha_1980A 2 Replies Last reply
          0
          • X xXiXx

            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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #5

            @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");
                });
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            4
            • X xXiXx

              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.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              Qt has to stay free or it will die.

              1 Reply Last reply
              2

              • Login

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