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: start a compiled c program and output to textBrowser in Linux
Forum Updated to NodeBB v4.3 + New Features

QProcess: start a compiled c program and output to textBrowser in Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
executec programqprocesstextbrowserlinux
13 Posts 6 Posters 4.2k Views 3 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.
  • N Offline
    N Offline
    Naim
    wrote on 15 Dec 2017, 15:36 last edited by
    #1

    I have search and looked at other examples but nothing seems to work.
    Here are where I've referred to:
    http://www.qtforum.org/article/3079/howto-start-an-external-program-from-a-qt-application.html
    http://doc.qt.io/qt-5/qprocess.html
    and a few others

    Here's what I have in my project:
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    main.cpp

    #include "mainwindow.h"
    #include <QApplication>
    #include <QPushButton>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QProcess>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        QProcess proc;
        proc.start("/root/Desktop/a.out");
    
        proc.waitForFinished();
        QString output = proc.readAllStandardOutput();
        ui->textBrowser->setText(output);
    }
    

    mainwindow.ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>683</width>
        <height>519</height>
       </rect>
      </property>
      <property name="maximumSize">
       <size>
        <width>1112</width>
        <height>625</height>
       </size>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <widget class="QTextBrowser" name="textBrowser">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>50</y>
          <width>631</width>
          <height>191</height>
         </rect>
        </property>
       </widget>
       <widget class="QWidget" name="layoutWidget">
        <property name="geometry">
         <rect>
          <x>20</x>
          <y>10</y>
          <width>631</width>
          <height>30</height>
         </rect>
        </property>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QPushButton" name="pushButton">
           <property name="text">
            <string>Start pcap (100p)</string>
           </property>
          </widget>
         </item>
         <item>
          <spacer name="horizontalSpacer">
           <property name="orientation">
            <enum>Qt::Horizontal</enum>
           </property>
           <property name="sizeHint" stdset="0">
            <size>
             <width>200</width>
             <height>20</height>
            </size>
           </property>
          </spacer>
         </item>
        </layout>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>683</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    

    When I clicked the pushButton nothing happens but if I tried using Linux system's command in the QProcess such as

        QProcess proc;
        proc.start("ls -al");
    
        proc.waitForFinished();
        QString output = proc.readAllStandardOutput();
        ui->textBrowser->setText(output);
    

    It works where in the textBrowser it will list files/folders in the home directory.
    I've also tried with sh -c but still doesn't work.

        proc.start("sh", QStringList() << "-c" << "/root/Desktop/a.out");
    

    Is there way to execute a compiled c program using QProcess?
    I'm at lost

    J 1 Reply Last reply 15 Dec 2017, 19:14
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 15 Dec 2017, 15:53 last edited by
      #2

      Hi, welcome to the forum. Check the ProcessError to get a hint on what went wrong. Also, add a call to waitForStarted and see if it hangs. Judging by the pathname, your a.out is owned by the user root. I assume you're not logged in as root so you probably don't have the sufficient access rights to run that executable.

      N 1 Reply Last reply 16 Dec 2017, 01:53
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 15 Dec 2017, 16:27 last edited by mrjj
        #3

        Hi
        Besides what @Wieland suggests
        did you try open a shell and just type
        /root/Desktop/a.out

        and see if it can run ?

        in some cases it wants
        chmod +x a.out

        to even be allowed to run.
        (normally the compiler / linker does that for you)

        1 Reply Last reply
        1
        • N Naim
          15 Dec 2017, 15:36

          I have search and looked at other examples but nothing seems to work.
          Here are where I've referred to:
          http://www.qtforum.org/article/3079/howto-start-an-external-program-from-a-qt-application.html
          http://doc.qt.io/qt-5/qprocess.html
          and a few others

          Here's what I have in my project:
          mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          private slots:
              void on_pushButton_clicked();
          
          private:
              Ui::MainWindow *ui;
          };
          
          #endif // MAINWINDOW_H
          

          main.cpp

          #include "mainwindow.h"
          #include <QApplication>
          #include <QPushButton>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
          
              return a.exec();
          }
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QProcess>
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::on_pushButton_clicked()
          {
              QProcess proc;
              proc.start("/root/Desktop/a.out");
          
              proc.waitForFinished();
              QString output = proc.readAllStandardOutput();
              ui->textBrowser->setText(output);
          }
          

          mainwindow.ui

          <?xml version="1.0" encoding="UTF-8"?>
          <ui version="4.0">
           <class>MainWindow</class>
           <widget class="QMainWindow" name="MainWindow">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>683</width>
              <height>519</height>
             </rect>
            </property>
            <property name="maximumSize">
             <size>
              <width>1112</width>
              <height>625</height>
             </size>
            </property>
            <property name="windowTitle">
             <string>MainWindow</string>
            </property>
            <widget class="QWidget" name="centralWidget">
             <widget class="QTextBrowser" name="textBrowser">
              <property name="geometry">
               <rect>
                <x>20</x>
                <y>50</y>
                <width>631</width>
                <height>191</height>
               </rect>
              </property>
             </widget>
             <widget class="QWidget" name="layoutWidget">
              <property name="geometry">
               <rect>
                <x>20</x>
                <y>10</y>
                <width>631</width>
                <height>30</height>
               </rect>
              </property>
              <layout class="QHBoxLayout" name="horizontalLayout">
               <item>
                <widget class="QPushButton" name="pushButton">
                 <property name="text">
                  <string>Start pcap (100p)</string>
                 </property>
                </widget>
               </item>
               <item>
                <spacer name="horizontalSpacer">
                 <property name="orientation">
                  <enum>Qt::Horizontal</enum>
                 </property>
                 <property name="sizeHint" stdset="0">
                  <size>
                   <width>200</width>
                   <height>20</height>
                  </size>
                 </property>
                </spacer>
               </item>
              </layout>
             </widget>
            </widget>
            <widget class="QMenuBar" name="menuBar">
             <property name="geometry">
              <rect>
               <x>0</x>
               <y>0</y>
               <width>683</width>
               <height>25</height>
              </rect>
             </property>
            </widget>
            <widget class="QToolBar" name="mainToolBar">
             <attribute name="toolBarArea">
              <enum>TopToolBarArea</enum>
             </attribute>
             <attribute name="toolBarBreak">
              <bool>false</bool>
             </attribute>
            </widget>
            <widget class="QStatusBar" name="statusBar"/>
           </widget>
           <layoutdefault spacing="6" margin="11"/>
           <resources/>
           <connections/>
          </ui>
          
          

          When I clicked the pushButton nothing happens but if I tried using Linux system's command in the QProcess such as

              QProcess proc;
              proc.start("ls -al");
          
              proc.waitForFinished();
              QString output = proc.readAllStandardOutput();
              ui->textBrowser->setText(output);
          

          It works where in the textBrowser it will list files/folders in the home directory.
          I've also tried with sh -c but still doesn't work.

              proc.start("sh", QStringList() << "-c" << "/root/Desktop/a.out");
          

          Is there way to execute a compiled c program using QProcess?
          I'm at lost

          J Offline
          J Offline
          JonB
          wrote on 15 Dec 2017, 19:14 last edited by
          #4

          @Naim , @mrjj
          If you like trivia (you probably don't...), you can actually execute it without the execute bit set via the accepted answer to https://superuser.com/questions/341439/can-i-execute-a-linux-binary-without-the-execute-permission-bit-being-set :)

          M 1 Reply Last reply 15 Dec 2017, 19:23
          1
          • J JonB
            15 Dec 2017, 19:14

            @Naim , @mrjj
            If you like trivia (you probably don't...), you can actually execute it without the execute bit set via the accepted answer to https://superuser.com/questions/341439/can-i-execute-a-linux-binary-without-the-execute-permission-bit-being-set :)

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 15 Dec 2017, 19:23 last edited by
            #5

            @JNBarchan
            But does it set the bit ?
            chmod a-x /tmp/ls

            A 1 Reply Last reply 16 Dec 2017, 11:19
            0
            • ? A Former User
              15 Dec 2017, 15:53

              Hi, welcome to the forum. Check the ProcessError to get a hint on what went wrong. Also, add a call to waitForStarted and see if it hangs. Judging by the pathname, your a.out is owned by the user root. I assume you're not logged in as root so you probably don't have the sufficient access rights to run that executable.

              N Offline
              N Offline
              Naim
              wrote on 16 Dec 2017, 01:53 last edited by Naim
              #6

              @Wieland Yes I'm using root. As for ProcessError and waitForStarted I'll reply that later
              @mrjj I compiled a.out as the root user

              M 1 Reply Last reply 16 Dec 2017, 09:31
              0
              • N Naim
                16 Dec 2017, 01:53

                @Wieland Yes I'm using root. As for ProcessError and waitForStarted I'll reply that later
                @mrjj I compiled a.out as the root user

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 16 Dec 2017, 09:31 last edited by
                #7

                @Naim
                Ok, so you are logged in as root. so its not just rights issue.
                Hopefully ProcessError tells you why it cant run :)

                A 1 Reply Last reply 16 Dec 2017, 11:15
                0
                • M mrjj
                  16 Dec 2017, 09:31

                  @Naim
                  Ok, so you are logged in as root. so its not just rights issue.
                  Hopefully ProcessError tells you why it cant run :)

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 16 Dec 2017, 11:15 last edited by
                  #8

                  @Naim: As @mrjj already asked earlier before:

                  Can you run /root/Desktop/a.out from the shell? It might be, that /root is on a partition that is mounted as no-executable. Why has to program to reside below /root?

                  Qt has to stay free or it will die.

                  N 1 Reply Last reply 18 Dec 2017, 01:05
                  1
                  • M mrjj
                    15 Dec 2017, 19:23

                    @JNBarchan
                    But does it set the bit ?
                    chmod a-x /tmp/ls

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 16 Dec 2017, 11:19 last edited by
                    #9

                    @mrjj said in QProcess: start a compiled c program and output to textBrowser in Linux:

                    @JNBarchan
                    But does it set the bit ?
                    chmod a-x /tmp/ls

                    chmod a-x /tmp/ls clears the executable bit ;)

                    Qt has to stay free or it will die.

                    M 1 Reply Last reply 16 Dec 2017, 11:22
                    1
                    • A aha_1980
                      16 Dec 2017, 11:19

                      @mrjj said in QProcess: start a compiled c program and output to textBrowser in Linux:

                      @JNBarchan
                      But does it set the bit ?
                      chmod a-x /tmp/ls

                      chmod a-x /tmp/ls clears the executable bit ;)

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 16 Dec 2017, 11:22 last edited by
                      #10

                      @aha_1980
                      ahh of course its -x :)
                      my brain said a TO x (facepalm)
                      So it can run without it using LD and some magic. Cool.

                      1 Reply Last reply
                      0
                      • A aha_1980
                        16 Dec 2017, 11:15

                        @Naim: As @mrjj already asked earlier before:

                        Can you run /root/Desktop/a.out from the shell? It might be, that /root is on a partition that is mounted as no-executable. Why has to program to reside below /root?

                        N Offline
                        N Offline
                        Naim
                        wrote on 18 Dec 2017, 01:05 last edited by
                        #11

                        @aha_1980 Yes I can run it in shell with ./root/Desktop/a.out. I'm using the root user on a virtual machine.

                        N 1 Reply Last reply 18 Dec 2017, 02:39
                        0
                        • N Naim
                          18 Dec 2017, 01:05

                          @aha_1980 Yes I can run it in shell with ./root/Desktop/a.out. I'm using the root user on a virtual machine.

                          N Offline
                          N Offline
                          Naim
                          wrote on 18 Dec 2017, 02:39 last edited by
                          #12

                          I may have find the problem here. My C program a.out runs indefinitely and textBrowser only shows the output once my program have finished executing. I've tried to run another C program that accepts command line input to loop how many times. Its output shows up in textBrowser after it has done looping.

                          J 1 Reply Last reply 18 Dec 2017, 05:27
                          0
                          • N Naim
                            18 Dec 2017, 02:39

                            I may have find the problem here. My C program a.out runs indefinitely and textBrowser only shows the output once my program have finished executing. I've tried to run another C program that accepts command line input to loop how many times. Its output shows up in textBrowser after it has done looping.

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 18 Dec 2017, 05:27 last edited by
                            #13

                            @Naim Use http://doc.qt.io/qt-5/qprocess.html#readyReadStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readyReadStandardError to get notifications each time the process writes something to stdout or stderr and read it using http://doc.qt.io/qt-5/qprocess.html#readAllStandardOutput and http://doc.qt.io/qt-5/qprocess.html#readAllStandardError. Do not call waitForFinished, just make sure your proc does not go out of scope (make it class member).

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

                            1 Reply Last reply
                            3

                            10/13

                            16 Dec 2017, 11:22

                            • Login

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