Results 1 to 2 of 2

Thread: Need help in Qthread

  1. #1
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Need help in Qthread

    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 .

  2. #2
    Join Date
    Feb 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help in Qthread

    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:B_openScript()
    {
    //FileInfo info;
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
    fileName,
    tr("All (*.*)"));
    // info.setFile(fileName);
    lineEdit_scriptName->insert(fileName);
    }


    void testCaseFm:B_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:rocessFinished(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:rocessError( 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.

Similar Threads

  1. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2008, 17:43
  2. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  3. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  4. QThread exec proplem to stop...
    By patrik08 in forum Qt Programming
    Replies: 29
    Last Post: 21st May 2007, 07:51
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.