• 6298阅读
  • 4回复

QProcess的问题 [复制链接]

上一主题 下一主题
离线ggtmp
 

只看楼主 倒序阅读 楼主  发表于: 2007-07-02
QProcess的问题
— 本帖被 XChinux 执行加亮操作(2008-07-18) —
我的程序
void sysmoniDialog::readFromStdout()
{
    // Read and process the data.
    // Bear in mind that the data might be output in chunks.
/*
    QFile file("dir.txt");
    if(!file.open(IO_WriteOnly))
    {
        //ioError(in,tr("cannot open file dir.txt for reading));
        QMessageBox *box = new QMessageBox("Dir Results","cannot open file dir.txt for writing.",QMessageBox::Information,QMessageBox::Ok,0,0);
        box->show();
        exit(0);
    }

    QStringList lines;
    lines.append(process->readStdout());
   
    QTextStream stream( &file );
    for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
        stream << *it << "\n";
    file.close();
*/
    textEdit1->append(p->readStdout());
}


void sysmoniDialog::GetProState()
{
   
    p = new QProcess(this);

    p->addArgument("more");
    p->addArgument("pro.txt");
    p->start();
    connect( p, SIGNAL(readyReadStdout()),this, SLOT(readFromStdout()) );
    /*
    QStringList command;
    command.append(tr("more"));
    command += " pro.txt";
    process->start(&command);
*/


    if (p->normalExit())
    {
        QMessageBox *box = new QMessageBox("Dir Results","If you see this message,the process dir is suceed.",QMessageBox::Information,QMessageBox::Ok,0,0);
        box->show();
    }
    else
    {
        QMessageBox *box = new QMessageBox("Dir Results","If you see this message,the process dir is failed.",QMessageBox::Information,QMessageBox::Ok,0,0);
        box->show();
    }
    delete p;
    p = 0;
}

执行时,QProcess p执行时,是抱错的!
各位请指点一下!
[ 此贴被XChinux在2008-07-18 13:45重新编辑 ]
离线mileden

只看该作者 1楼 发表于: 2007-07-02
或许是进程还未执行完毕就读了 normalExit() 。
离线mileden

只看该作者 2楼 发表于: 2007-07-02
在  if (p->normalExit())
前面插入以下代码试一下

    if(!p->waitForStarted())
        qDebug() << "failed to start the program ";
    if(!p->waitForFinished())
    {
        qDebug() << "timeout!";
        p->kill();
    }
离线mileden

只看该作者 3楼 发表于: 2007-07-02
    p = new QProcess(this);
    connect( p, SIGNAL(readyReadStdout()),this, SLOT(readFromStdout()) );
    QStringList command;
    command << "more" << "pro.txt";
    process->start(command);  //这是引用传递参数
    p->start();
离线ggtmp

只看该作者 4楼 发表于: 2007-07-03
谢谢了,各位!
快速回复
限100 字节
 
上一个 下一个