返回介绍

Starting processes with IO redirection

发布于 2019-10-04 14:58:10 字数 2675 浏览 966 评论 0 收藏 0

This example shows you how to start other processes with Qt and how IO redirection is done. The example tries to start the uic (a tool that comes with the Qt Designer) on a certain ui file and displays the output of the command.


Implementation (process.cpp):

/****************************************************************************
** $Id:  qt/process.cpp   3.0.5   edited Oct 12 2001 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qobject.h>
#include <qprocess.h>
#include <qvbox.h>
#include <qtextview.h>
#include <qpushbutton.h>
#include <qapplication.h>
#include <qmessagebox.h>

#include <stdlib.h>

class UicManager : public QVBox
{
    Q_OBJECT

public:
    UicManager();
    ~UicManager() {}

public slots:
    void readFromStdout();
    void scrollToTop();

private:
    QProcess *proc;
    QTextView *output;
    QPushButton *quitButton;
};

UicManager::UicManager()
{
    // Layout
    output = new QTextView( this );
    quitButton = new QPushButton( tr("Quit"), this );
    connect( quitButton, SIGNAL(clicked()),
            qApp, SLOT(quit()) );
    resize( 500, 500 );

    // QProcess related code
    proc = new QProcess( this );

    // Set up the command and arguments.
    // On the command line you would do:
    //   uic -tr i18n "small_dialog.ui"
    proc->addArgument( "uic" );
    proc->addArgument( "-tr" );
    proc->addArgument( "i18n" );
    proc->addArgument( "small_dialog.ui" );

    connect( proc, SIGNAL(readyReadStdout()),
            this, SLOT(readFromStdout()) );
    connect( proc, SIGNAL(processExited()),
            this, SLOT(scrollToTop()) );

    if ( !proc->start() ) {
        // error handling
        QMessageBox::critical( 0,
                tr("Fatal error"),
                tr("Could not start the uic command."),
                tr("Quit") );
        exit( -1 );
    }
}

void UicManager::readFromStdout()
{
    // Read and process the data.
    // Bear in mind that the data might be output in chunks.
    output->append( proc->readStdout() );
}

void UicManager::scrollToTop()
{
    output->setContentsPos( 0, 0 );
}

int main( int argc, char **argv )
{
    QApplication a( argc, argv );
    UicManager manager;
    a.setMainWidget( &manager );
    manager.show();
    return a.exec();
}

#include "process.moc"

See also QProcess Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文