1

I'm trying to get Qt creator to print a user input by using a push button on an UI into the terminal. As of now, the code is executable on the terminal via human input. Here is the code:

  void MainWindow::on_pushButton_clicked()
{
    QProcess::execute("/catkin_ws/devel/lib/submodbus");

    system("cd catkin_ws/devel/lib/submodbus");
    system("./submodbus_node");

}

Current output when using the code

Output via human input

The versions i'm running on are: -Ubuntu 16.04 -QT Creator 3.5.1

5
  • change cd catkin_ws/devel/lib/submodbus to cd /full_path/of/catkin_ws/devel/lib/submodbus Commented Dec 10, 2018 at 3:03
  • Thanks for the reply, but no it still says not found Commented Dec 10, 2018 at 3:54
  • remove system("cd catkin_ws/devel/lib/submodbus"); system("./submodbus_node"); and change QProcess::execute("/catkin_ws/devel/lib/submodbus"); to QProcess::execute("/full/path/of/catkin_ws/devel/lib/submodbus/submodbus_node"); Commented Dec 10, 2018 at 3:57
  • This exe file is for a robot operation and I am currently using the executable file named submodbus_node from here link but trying to open it directly from here does not do anything, i have to source its location and type ./submodbus node into the console to actually run this exe file. I have tried what you suggested above but there were no results Commented Dec 10, 2018 at 4:33
  • Are you trying to change the working directory first then execute the program? Commented Dec 10, 2018 at 5:00

1 Answer 1

3

system can't change the current directory globally. but could use like this:

system("cd /catkin_ws/devel/lib/submodbus && ./submodbus_node");

or using QProcess::setProgram with QProcess::setWorkingDirectory

QProcess p;
p.setProgram("submodbus_node");
//p.setArguments(QStringList()<<args); // if you need
p.setWorkingDirectory("/catkin_ws/devel/lib/submodbus");
p.start();

or QDir::setCurrent

QDir::setCurrent("/catkin_ws/devel/lib/submodbus");
QProcess::startDetached("submodbus_node");

Test demo, create three files in the parent directory:

#include <QApplication>
#include <QProcess>
#include <QDir>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    system("cd ../ && touch test1.txt");

    QProcess p;
    p.setProgram("touch");
    p.setArguments(QStringList()<<"test2.txt");
    p.setWorkingDirectory("../");
    p.start();

    QDir::setCurrent("../");
    QProcess::startDetached("touch test3.txt");

    return a.exec();
}
Sign up to request clarification or add additional context in comments.

15 Comments

Hi thanks for the reply, I tried your second method and found out that there is no member called setProgram in QProcess
@AvremTan What's the Qt version? (not QtCreator). the second one requires Qt 5.1.
I'm running it on Qt version 5.5.1
@AvremTan umm...that's weird. I am using the environment exactly same as you. What's the error message?
@AvremTan Could you post the line where you invoke the function?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.