PDA

View Full Version : Executing 2 or more shell scripts simultaneously using qt



karthikkm1987
2nd December 2011, 06:17
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.

Lykurg
2nd December 2011, 08:17
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.

myta212
2nd December 2011, 11:10
Hi,
You can use QProcess to solve your problem.
Create two QProcess and connect every that process to each editor.


/* create QProcess object */
proc1= new QProcess();
proc1->start(stringprocess1);

/* show output */
connect(proc1, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage1()) );
connect(proc1, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage1()) );
/*------------------------------------------------------------------------------------------*/

/* create QProcess object */
proc2= new QProcess();
proc2->start(stringprocess2);

/* show output */
connect(proc2, SIGNAL(readyReadStandardOutput()),this, SLOT(rightMessage2()) );
connect(proc2, SIGNAL(readyReadStandardError()), this, SLOT(wrongMessage2()) );



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

karthikkm1987
2nd December 2011, 14:01
Thank you I will Try that QProcess example you have given.

karthikkm1987
8th December 2011, 05:20
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.


71597160

ChrisW67
8th December 2011, 09:27
Please edit your post and put your code into
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.

karthikkm1987
8th December 2011, 12:20
Hai I have edited as you asked please check my previous post.

ChrisW67
9th December 2011, 05:55
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...

karthikkm1987
13th December 2011, 11:00
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>

Spitfire
13th December 2011, 11:21
Could you please edit your post and wrap the code inside [code] tags?
Without them it's just unreadable mess...

karthikkm1987
13th December 2011, 12:31
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

karthikkm1987
19th December 2011, 14:05
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.


#include<stdio.h>
main()
{
int i=0;
for(;;)
{
printf("hello %d\n",i);
i++;
sleep(1);
}
}