Results 1 to 12 of 12

Thread: Executing 2 or more shell scripts simultaneously using qt

  1. #1
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Executing 2 or more shell scripts simultaneously using qt

    Hai I am a newbee in Qt i have done few basic test cases like notepad and others.
    Can anyone tell me is there anyway to execute 2 shell scripts simultaneously and
    taking the result and putting onto 2 separate text editors respectively.
    The shell scripts are in infinite loops.
    Please help me.
    Last edited by karthikkm1987; 2nd December 2011 at 06:38.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Have a look at QProcess. It works asynchron, so you don't have to do any special. Just use the signal and slot mechanism with it.

  3. #3
    Join Date
    Nov 2006
    Location
    indonesia
    Posts
    55
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 11 Times in 11 Posts

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Hi,
    You can use QProcess to solve your problem.
    Create two QProcess and connect every that process to each editor.
    Qt Code:
    1. /* create QProcess object */
    2. proc1= new QProcess();
    3. proc1->start(stringprocess1);
    4.  
    5. /* show output */
    6. connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) );
    7. connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage1()) );
    8. /*------------------------------------------------------------------------------------------*/
    9.  
    10. /* create QProcess object */
    11. proc2= new QProcess();
    12. proc2->start(stringprocess2);
    13.  
    14. /* show output */
    15. connect(proc2, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage2()) );
    16. connect(proc2, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage2()) );
    To copy to clipboard, switch view to plain text mode 

    You can get simple tutorial how to use QProcess at here http://toto-share.com/2011/07/qt-qprocess-tutorial.
    Are this method solve your problem?

    Best regards,

    myta

  4. #4
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Thank you I will Try that QProcess example you have given.

  5. #5
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Hai I am able to compile the program.But The gui is not running the shell scripts.
    here is the code.

    modbusarm.pro:--

    #-------------------------------------------------
    #
    # Project created by QtCreator 2011-12-05T10:21:56
    #
    #-------------------------------------------------

    QT += core gui

    TARGET = modbusarm
    TEMPLATE = app


    SOURCES += main.cpp


    HEADERS += modbusarm.h ui_modbusarm.h

    FORMS += modbusarm.ui

    header files:-
    modbusarm.h


    #ifndef MODBUSARM_H
    #define MODBUSARM_H

    #include<QDialog>
    #include<QtGui>
    #include<QProcess>
    #include<QMainWindow>
    #include"ui_modbusarm.h"
    #include<QByteArray>

    //#include<Qt3Support/Q3Process>

    class modbusarmublic QMainWindow,public Ui_modbusarm
    {
    Q_OBJECT

    public:

    QProcess* proc1;
    QProcess* proc2;
    modbusarm()
    {
    setupUi(this);
    /* create QProcess object */

    proc1= new QProcess();
    QString stringproc1="/home/karthik/Desktop/hai ";

    proc1->start(stringproc1);



    /* show output */
    // connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(readAllStandardOutput()) );

    connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) );

    // connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage1()) );

    /*------------------------------------------------------------------------------------------*/



    /* create QProcess object */
    proc2= new QProcess();
    QString stringproc2="/home/karthik/Desktop/hai";
    proc2->start(stringproc2);

    // connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(readAllStandardOutput()) );

    /* show output */

    connect(proc2, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage2()) );

    //connect(proc2, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage2()) );


    }

    void rightMessage1()
    {
    QByteArray data = proc1->readAllStandardOutput();
    //arm1->setText(QString data);
    QString text = arm1->text() + QString(data);
    arm1->setText(text);


    }
    void rightMessage2()
    {
    QByteArray data1 = proc2->readAllStandardOutput();


    QString text1 = arm2->text() + QString(data1);
    arm2->setText(text1);


    }


    // ~modbusarm();

    private:
    //Ui::modbusarm *ui;
    };

    #endif // MODBUSARM_H

    modbusarm.cpp: source file

    #include "modbusarm.h"
    #include "ui_modbusarm.h"

    modbusarm::modbusarm(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::modbusarm)
    {
    ui->setupUi(this);
    }

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

    main.cpp

    #include <QApplication>
    #include "modbusarm.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    modbusarm* w=new modbusarm();
    w->show();

    return a.exec();
    }


    the shell script is

    #!/bin/bash
    c=0
    while [ 1 ]
    do

    ((c++))
    echo " hai $c times "
    sleep 1
    done
    #######################################

    Please Help Me.
    Please Help Me.
    The output of shell scripts should be continuously outputted at Line edit of GU I have attached the UI file also.


    modbusarm.uimodbusarm.tar.gz
    Last edited by karthikkm1987; 8th December 2011 at 10:01.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Please edit your post and put your code into [code][/code] tags.

    Here are some tips:
    • Post the actual buildable code in an easily recognised form (i.e. not a gzipped, gzipped, tar file)
    • Did I mention that your code should compile before you post it?
    • At the moment the modbusarm.cpp file is not part of the project.
    • The ui_*h file should not be listed in HEADERS.
    • The modbusarm.h and modbusarm.cpp files contain competing implementations. Generally, the header file should contain only declarations and the cpp file the implementation.
    • rightMessage1() and rightMessage2() are not declared as slots as far as I can see. If the program compile and ran your connect() calls would generate an error message.

  7. #7
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Hai I have edited as you asked please check my previous post.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    No you didn't. Anyway, please see the bullet points in my last post for guidance toward your solution. Until you have code that will compile...

  9. #9
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    hai ChrisW67 thanks for your help.this program given below executes well with shell scripts.
    the shell scripts run endlessly until i close it.
    But I want to ask you is there any way that a shell script containing a .c executable file is executed in the shell script.
    when I tried with the executable c program ,it didnt run.
    I will give you a general example of the script.
    hellosh:-
    #!/bin/bash
    echo "executing ./hey1"
    ./hey1
    hello.c:-
    #include<stdio.h>
    main()
    {
    int i=0;
    for(;
    {
    printf("hello %d\n",i);
    i++;
    sleep(1);
    }
    }

    and i did
    cc hello.c -o hey1


    modbusarm1.pro:-
    #-------------------------------------------------
    #
    # Project created by QtCreator 2011-12-09T11:28:02
    #
    #-------------------------------------------------

    QT += core gui

    TARGET = modbusarm1
    TEMPLATE = app


    SOURCES += main.cpp\
    modbusarm1.cpp

    HEADERS += modbusarm1.h


    FORMS += modbusarm1.ui

    RESOURCES +=

    main.cpp:-

    #include <QtGui/QApplication>
    #include "modbusarm1.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    modbusarm1 w;
    w.show();

    return a.exec();
    }

    modbusarm1.h:-
    #ifndef MODBUSARM1_H
    #define MODBUSARM1_H
    #include<QProcess>
    #include <QWidget>
    //#include<QtGui>

    namespace Ui {
    class modbusarm1;
    }

    class modbusarm1 : public QWidget
    {
    Q_OBJECT

    public:
    modbusarm1(QWidget *parent = 0);
    ~modbusarm1();

    QProcess* proc1;
    QProcess* proc2;
    // QTextEdit* arm_1;
    // QTextEdit* arm_2;

    public slots:
    void rightMessage1();
    void rightMessage2();


    private:
    Ui::modbusarm1 *ui;
    };

    #endif // MODBUSARM1_H


    modbusarm1.cpp:-

    #include "modbusarm1.h"
    #include "ui_modbusarm1.h"
    #include<QWidget>
    #include<QProcess>
    #include<QIODevice>
    //#include<QtGui>

    modbusarm1::modbusarm1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::modbusarm1)
    {
    ui->setupUi(this);
    //QProcess* proc1;
    //QProcess* proc2;
    proc1= new QProcess();
    //QString stringproc1="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcserver1";
    QString stringproc1="/home/karthik/hellosh";
    // /home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcserver1sh
    proc1->start(stringproc1);
    /* show output */
    // connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(readAllStandardOutput()) );
    connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) );

    // connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage1()) );

    /*------------------------------------------------------------------------------------------*/
    /* create QProcess object */
    proc2= new QProcess();
    // QString stringproc2="/home/karthik/Project2ndyearfinal/modbus_multiple_devices/adcserver2";
    QString stringproc2="/home/karthik/hello";

    proc2->start(stringproc2);
    // connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(readAllStandardOutput()) );
    /* show output */
    connect(proc2, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage2()) );
    //connect(proc2, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage2()) );
    connect(ui->close,SIGNAL(clicked()),proc1,SLOT(kill()));
    connect(ui->close,SIGNAL(clicked()),proc2,SLOT(kill()));

    }

    void modbusarm1::rightMessage1()
    {
    QByteArray data = proc1->readAllStandardOutput();

    // arm_1->setText(QString data);
    QString text2 = ui->arm_1->text() + QString(data);
    ui->arm_1->setText(text2);
    // connect(ui->close,SIGNAL(clicked()),proc1,SLOT(proc1->kill());
    // connect(ui->close,SIGNAL(clicked()),proc1,SLOT(kill()));


    }
    void modbusarm1::rightMessage2()
    {
    QByteArray data1 = proc2->readAllStandardOutput();
    // arm_2->setText(QString data1);
    QString text1 = ui->arm_2->text() + QString(data1);

    ui->arm_2->setText(text1);
    // connect(ui->close,SIGNAL(clicked()),proc1,SLOT(proc2->close());

    // connect(ui->close,SIGNAL(clicked()),proc2,SLOT(close()));

    }

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

    modbusarm1.ui:-
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>modbusarm1</class>
    <widget class="QWidget" name="modbusarm1">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>modbusarm1</string>
    </property>
    <widget class="QLabel" name="Friendly_Arm1">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>50</y>
    <width>141</width>
    <height>31</height>
    </rect>
    </property>
    <property name="text">
    <string>Friendly Arm1</string>
    </property>
    </widget>
    <widget class="QLabel" name="Friendly_Arm2">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>150</y>
    <width>141</width>
    <height>31</height>
    </rect>
    </property>
    <property name="text">
    <string>Friendly Arm2</string>
    </property>
    </widget>
    <widget class="QLineEdit" name="arm_1">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>90</y>
    <width>171</width>
    <height>31</height>
    </rect>
    </property>
    </widget>
    <widget class="QLineEdit" name="arm_2">
    <property name="geometry">
    <rect>
    <x>120</x>
    <y>190</y>
    <width>171</width>
    <height>31</height>
    </rect>
    </property>
    </widget>
    <widget class="QPushButton" name="close">
    <property name="geometry">
    <rect>
    <x>150</x>
    <y>240</y>
    <width>97</width>
    <height>31</height>
    </rect>
    </property>
    <property name="text">
    <string>Close</string>
    </property>
    </widget>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections>
    <connection>
    <sender>close</sender>
    <signal>clicked()</signal>
    <receiver>modbusarm1</receiver>
    <slot>close()</slot>
    <hints>
    <hint type="sourcelabel">
    <x>231</x>
    <y>260</y>
    </hint>
    <hint type="destinationlabel">
    <x>306</x>
    <y>250</y>
    </hint>
    </hints>
    </connection>
    </connections>
    </ui>

  10. #10
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 106 Times in 103 Posts

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    Could you please edit your post and wrap the code inside [code] tags?
    Without them it's just unreadable mess...

  11. #11
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    sorry spitfire for the code mess.
    Anyways I came to know how to execute c program in shell script I had to use /path/program instead of ./path/program

  12. #12
    Join Date
    Dec 2011
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11
    Thanks
    1

    Default Re: Executing 2 or more shell scripts simultaneously using qt

    hai is it possible for seperating the outputs which i have got.
    I mean is it possible to extract the required data.?
    Like is it possible to output hello 1,hello 3 .....so on in one line edit
    and print hello 2,hello 4,............so on in another line edit.As shown in the below code it is in infinite loop.
    after the program is executed using QProcess.

    Qt Code:
    1. #include<stdio.h>
    2. main()
    3. {
    4. int i=0;
    5. for(;;)
    6. {
    7. printf("hello %d\n",i);
    8. i++;
    9. sleep(1);
    10. }
    To copy to clipboard, switch view to plain text mode 
    }

Similar Threads

  1. sorting two QStringLists simultaneously
    By hokie2012 in forum Qt Programming
    Replies: 7
    Last Post: 6th September 2010, 08:45
  2. Zooming two plots simultaneously
    By Ban-chan in forum Qwt
    Replies: 2
    Last Post: 2nd September 2009, 20:42
  3. Executing SQL scripts from a file
    By William Wilson in forum Qt Programming
    Replies: 2
    Last Post: 30th August 2007, 20:28
  4. more network connections simultaneously
    By cmeliak in forum Qt Programming
    Replies: 9
    Last Post: 8th June 2006, 01:17
  5. How to run shell scripts ?
    By npc in forum Qt Programming
    Replies: 5
    Last Post: 22nd May 2006, 12:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.