PDA

View Full Version : Need help in Qthread



skumar434
4th March 2009, 17:18
Hi All,
I have designed a page which will take the script name and run it . I am currently using Qprocess to run the script and printing the out put on the textEdit.
The code works fine when I give a small script , but the GUI hungs and I saw SEGV when I tried to run a simple script which only do "ls" in a loop for 300 times .

I belive all the error is at the time of reading / printing of the data on the text Edit.

I have very less knowledge on both debugging and QT , I tried alot to find the issue but when run with GDB I found this


gdb) where
#0 0x00aa27a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
#1 0x03e00a41 in ___newselect_nocancel () from /lib/tls/libc.so.6
#2 0x0060b1cd in QProcessManager::run (this=0x9fdbb88) at io/qprocess_unix.cpp:301
#3 0x0053b768 in QThreadPrivate::start (arg=0x9fdbb88) at thread/qthread_unix.cpp:185
#4 0x00464371 in start_thread () from /lib/tls/libpthread.so.0
#5 0x03e07ffe in clone () from /lib/tls/libc.so.6
(gdb) f 0
#0 0x00aa27a2 in _dl_sysinfo_int80 () from /lib/ld-linux.so.2
(gdb) f 1
#1 0x03e00a41 in ___newselect_nocancel () from /lib/tls/libc.so.6
(gdb) f 2
#2 0x0060b1cd in QProcessManager::run (this=0x9fdbb88) at io/qprocess_unix.cpp:301
301 int nselect = select(qt_qprocess_deadChild_pipe[0] + 1, &readset, 0, 0, 0);
Current language: auto; currently c++




As my above approch is blocking the GUI , Now I am need a way to read the out put of the process through a thread ... Can any body tell me how to create a thread which will take the script name as a argument and run it .

skumar434
4th March 2009, 17:21
This is my Qprocess code



#include <QtGui>

#include "testcasefm.h"


testCaseFm::testCaseFm( QWidget * parent ): QWidget( parent )
{
setupUi(this);
scriptProcess = new QProcess( this );
label_targetName->setFocus();

testCaseFm::init();
//connect(label_targetName ,SIGNAL(setFocus()),this,SLOT(init()));
connect(pushButton_openScript ,SIGNAL(clicked()),this,SLOT(pB_openScript()));
connect(pushButton_runScript ,SIGNAL(clicked()),this,SLOT(pB_runScript()));
connect(pushButton_stopScript ,SIGNAL(clicked()),this,SLOT(pB_stopScript()));
//connect(pushButton_closeTestcasefm ,SIGNAL(clicked()),this,SLOT(pB_close()));
connect(scriptProcess ,SIGNAL(readyReadStandardOutput()),this,SLOT(updat eOutputTextEdit()));
//connect(scriptProcess ,SIGNAL(readyReadStandardError()),this,SLOT(update ErrorOutputTextEdit()));
connect(scriptProcess ,SIGNAL(finished(int, QProcess::ExitStatus)),this,SLOT(processFinished(i nt, QProcess::ExitStatus)));
connect(scriptProcess ,SIGNAL(error(QProcess::ProcessError)),this,SLOT(p rocessError(QProcess::ProcessError)));

}

testCaseFm::~testCaseFm()
{
close();
}

void testCaseFm::init()
{
QString vtlip;
QFile file("/root/test/cfg/ATSCfg");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
return;
QTextStream in(&file);
while(!in.atEnd()){
QString str = in.readLine();
if( str.contains("VTL_IP",Qt::CaseSensitive))
vtlip = str.section('"',1,1); }

label_vtlName->setText(vtlip);

file.close();
}


void testCaseFm::pB_openScript()
{
//FileInfo info;
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
fileName,
tr("All (*.*)"));
// info.setFile(fileName);
lineEdit_scriptName->insert(fileName);
}


void testCaseFm::pB_runScript()
{
QDir d;
textEdit->clear();
QString scriptName = lineEdit_scriptName->text();
if(!d.setCurrent("/root/ATS/testtools/ATS_Framework/ATS/"))
qDebug() << "failed";

//scriptProcess->setWorkingDirectory(scriptName);
//QString args;
if( lineEdit_scriptName->text().length() > 0 ){
// args << scriptName;
scriptProcess->start(scriptName);
// pushButton_runScript->setDisable(true);
}
else {
//qdebug << "Warning : No script name mentioned";
QMessageBox::warning(this,"Waring",
"Please specify the script name or path and then start\n"
"OK\n");
}
}


void testCaseFm::updateOutputTextEdit()
{
QByteArray newData = scriptProcess->readAllStandardOutput();
QString text = textEdit->toPlainText()
+QString::fromLocal8Bit(newData);
textEdit->append(text);
/*
newData = scriptProcess->readAllStandardError();
QString text_error = textEdit->toPlainText()
+QString::fromLocal8Bit(newData);
textEdit->append(text_error);
*/

//connect(scriptProcess ,SIGNAL(readyReadStandardOutput()),this,SLOT(updat eOutputTextEdit()));
}


void testCaseFm::updateErrorOutputTextEdit()
{
textEdit->append("----------Stard Error---------");
QByteArray newData = scriptProcess->readAllStandardOutput();
newData = scriptProcess->readAllStandardError();
QString text_error = textEdit->toPlainText()
+QString::fromLocal8Bit(newData);
//textEdit->append(text_error);
}


void testCaseFm::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
QDir tcfd("/root/tcf_logs");
QDateTime date;
date = QDateTime::currentDateTime();
if ( exitStatus == QProcess::CrashExit ) {
textEdit->append(tr("Test Case Crashed")); else if ( exitCode != 0 ) {
textEdit->append(tr("Test script failed "));
}
else {
textEdit->append(tr("Test Script Completed successfully"));
if(!tcfd.exists()){
tcfd.mkdir("/root/tcf_logs");
}
// QStringList args;
// args >> "/root/tcf_logs/" >> lineEdit_scriptName->text() >> date.currentDateTime();
QFileInfo info;
info.setFile(lineEdit_scriptName->text());
QString filename = QString("%1%2%3%4").arg("/root/tcf_logs/").arg(info.fileName()).arg("_").arg(date.toString(Qt::TextDate));
qDebug() << filename;
QFile file(filename);
if (!file.open(QFile::WriteOnly | QFile::Text)) {
QMessageBox::warning(this, tr("TCF"),
tr("Cannot write file %1:\n%2.")
.arg(filename)
.arg(file.errorString()));
return;
}

QTextStream out(&file);
QApplication::setOverrideCursor(Qt::WaitCursor);
out << textEdit->toPlainText();
QApplication::restoreOverrideCursor();

// statusBar()->showMessage(tr("Saved '%1'").arg(fileName), 2000);

}
// pushButton_runScript->setEnable(true);
void testCaseFm::processError( QProcess::ProcessError error )
{

if ( error == QProcess::FailedToStart ){
textEdit->append(tr(" Script was not able to start ..Please recheckthe path/filename"));
// pushButton_runScript->setEnable(true);
}
}




I would really appriciate if any body could at least tell me how ro proceed with this using Qthread.