| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> | 
|---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/process/process.doc:5 --> | 
|---|
| 3 | <html> | 
|---|
| 4 | <head> | 
|---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | 
|---|
| 6 | <title>Starting processes with IO redirection</title> | 
|---|
| 7 | <style type="text/css"><!-- | 
|---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; } | 
|---|
| 9 | a:link { color: #004faf; text-decoration: none } | 
|---|
| 10 | a:visited { color: #672967; text-decoration: none } | 
|---|
| 11 | body { background: #ffffff; color: black; } | 
|---|
| 12 | --></style> | 
|---|
| 13 | </head> | 
|---|
| 14 | <body> | 
|---|
| 15 |  | 
|---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%"> | 
|---|
| 17 | <tr bgcolor="#E5E5E5"> | 
|---|
| 18 | <td valign=center> | 
|---|
| 19 | <a href="index.html"> | 
|---|
| 20 | <font color="#004faf">Home</font></a> | 
|---|
| 21 | | <a href="classes.html"> | 
|---|
| 22 | <font color="#004faf">All Classes</font></a> | 
|---|
| 23 | | <a href="mainclasses.html"> | 
|---|
| 24 | <font color="#004faf">Main Classes</font></a> | 
|---|
| 25 | | <a href="annotated.html"> | 
|---|
| 26 | <font color="#004faf">Annotated</font></a> | 
|---|
| 27 | | <a href="groups.html"> | 
|---|
| 28 | <font color="#004faf">Grouped Classes</font></a> | 
|---|
| 29 | | <a href="functions.html"> | 
|---|
| 30 | <font color="#004faf">Functions</font></a> | 
|---|
| 31 | </td> | 
|---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Starting processes with IO redirection</h1> | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | <p> | 
|---|
| 36 | <p> This example shows you how to start other processes with Qt and how | 
|---|
| 37 | IO redirection is done. The example tries to start the uic (a tool | 
|---|
| 38 | that comes with the Qt Designer) on a certain ui file and displays the | 
|---|
| 39 | output of the command. | 
|---|
| 40 | <p> <hr> | 
|---|
| 41 | <p> Implementation (process.cpp): | 
|---|
| 42 | <p> <pre>/**************************************************************************** | 
|---|
| 43 | ** $Id: process-example.html 2051 2007-02-21 10:04:20Z chehrlic $ | 
|---|
| 44 | ** | 
|---|
| 45 | ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved. | 
|---|
| 46 | ** | 
|---|
| 47 | ** This file is part of an example program for Qt.  This example | 
|---|
| 48 | ** program may be used, distributed and modified without limitation. | 
|---|
| 49 | ** | 
|---|
| 50 | *****************************************************************************/ | 
|---|
| 51 |  | 
|---|
| 52 | #include <<a href="qobject-h.html">qobject.h</a>> | 
|---|
| 53 | #include <<a href="qprocess-h.html">qprocess.h</a>> | 
|---|
| 54 | #include <<a href="qvbox-h.html">qvbox.h</a>> | 
|---|
| 55 | #include <<a href="qtextview-h.html">qtextview.h</a>> | 
|---|
| 56 | #include <<a href="qpushbutton-h.html">qpushbutton.h</a>> | 
|---|
| 57 | #include <<a href="qapplication-h.html">qapplication.h</a>> | 
|---|
| 58 | #include <<a href="qmessagebox-h.html">qmessagebox.h</a>> | 
|---|
| 59 |  | 
|---|
| 60 | #include <stdlib.h> | 
|---|
| 61 |  | 
|---|
| 62 | class UicManager : public <a href="qvbox.html">QVBox</a> | 
|---|
| 63 | { | 
|---|
| 64 | <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a> | 
|---|
| 65 |  | 
|---|
| 66 | public: | 
|---|
| 67 | UicManager(); | 
|---|
| 68 | ~UicManager() {} | 
|---|
| 69 |  | 
|---|
| 70 | public slots: | 
|---|
| 71 | void readFromStdout(); | 
|---|
| 72 | void scrollToTop(); | 
|---|
| 73 |  | 
|---|
| 74 | private: | 
|---|
| 75 | <a href="qprocess.html">QProcess</a> *proc; | 
|---|
| 76 | <a href="qtextview.html">QTextView</a> *output; | 
|---|
| 77 | <a href="qpushbutton.html">QPushButton</a> *quitButton; | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | <a name="f204"></a>UicManager::UicManager() | 
|---|
| 81 | { | 
|---|
| 82 | // Layout | 
|---|
| 83 | output = new <a href="qtextview.html">QTextView</a>( this ); | 
|---|
| 84 | quitButton = new <a href="qpushbutton.html">QPushButton</a>( <a href="qobject.html#tr">tr</a>("Quit"), this ); | 
|---|
| 85 | <a href="qobject.html#connect">connect</a>( quitButton, SIGNAL(<a href="qbutton.html#clicked">clicked</a>()), | 
|---|
| 86 | qApp, SLOT(<a href="qapplication.html#quit">quit</a>()) ); | 
|---|
| 87 | <a href="qwidget.html#resize">resize</a>( 500, 500 ); | 
|---|
| 88 |  | 
|---|
| 89 | // QProcess related code | 
|---|
| 90 | proc = new <a href="qprocess.html">QProcess</a>( this ); | 
|---|
| 91 |  | 
|---|
| 92 | // Set up the command and arguments. | 
|---|
| 93 | // On the command line you would do: | 
|---|
| 94 | //   uic -tr <a href="i18n.html#i18n">i18n</a> "small_dialog.ui" | 
|---|
| 95 | <a name="x97"></a>    proc-><a href="qprocess.html#addArgument">addArgument</a>( "uic" ); | 
|---|
| 96 | proc-><a href="qprocess.html#addArgument">addArgument</a>( "-tr" ); | 
|---|
| 97 | proc-><a href="qprocess.html#addArgument">addArgument</a>( "i18n" ); | 
|---|
| 98 | proc-><a href="qprocess.html#addArgument">addArgument</a>( "small_dialog.ui" ); | 
|---|
| 99 |  | 
|---|
| 100 | <a name="x100"></a>    <a href="qobject.html#connect">connect</a>( proc, SIGNAL(<a href="qprocess.html#readyReadStdout">readyReadStdout</a>()), | 
|---|
| 101 | this, SLOT(readFromStdout()) ); | 
|---|
| 102 | <a name="x98"></a>    <a href="qobject.html#connect">connect</a>( proc, SIGNAL(<a href="qprocess.html#processExited">processExited</a>()), | 
|---|
| 103 | this, SLOT(scrollToTop()) ); | 
|---|
| 104 |  | 
|---|
| 105 | <a name="x101"></a>    if ( !proc-><a href="qprocess.html#start">start</a>() ) { | 
|---|
| 106 | // error handling | 
|---|
| 107 | <a name="x96"></a>        QMessageBox::<a href="qmessagebox.html#critical">critical</a>( 0, | 
|---|
| 108 | <a href="qobject.html#tr">tr</a>("Fatal error"), | 
|---|
| 109 | <a href="qobject.html#tr">tr</a>("Could not start the uic command."), | 
|---|
| 110 | <a href="qobject.html#tr">tr</a>("Quit") ); | 
|---|
| 111 | exit( -1 ); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | void <a name="f205"></a>UicManager::readFromStdout() | 
|---|
| 116 | { | 
|---|
| 117 | // Read and process the data. | 
|---|
| 118 | // Bear in mind that the data might be output in chunks. | 
|---|
| 119 | <a name="x99"></a><a name="x103"></a>    output-><a href="qtextedit.html#append">append</a>( proc-><a href="qprocess.html#readStdout">readStdout</a>() ); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void <a name="f206"></a>UicManager::scrollToTop() | 
|---|
| 123 | { | 
|---|
| 124 | <a name="x102"></a>    output-><a href="qscrollview.html#setContentsPos">setContentsPos</a>( 0, 0 ); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | int main( int argc, char **argv ) | 
|---|
| 128 | { | 
|---|
| 129 | <a href="qapplication.html">QApplication</a> a( argc, argv ); | 
|---|
| 130 | UicManager manager; | 
|---|
| 131 | a.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &manager ); | 
|---|
| 132 | manager.<a href="qwidget.html#show">show</a>(); | 
|---|
| 133 | return a.<a href="qapplication.html#exec">exec</a>(); | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | #include "process.moc" | 
|---|
| 137 | </pre> | 
|---|
| 138 |  | 
|---|
| 139 | <p>See also <a href="qprocess-examples.html">QProcess Examples</a>. | 
|---|
| 140 |  | 
|---|
| 141 | <!-- eof --> | 
|---|
| 142 | <p><address><hr><div align=center> | 
|---|
| 143 | <table width=100% cellspacing=0 border=0><tr> | 
|---|
| 144 | <td>Copyright © 2007 | 
|---|
| 145 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a> | 
|---|
| 146 | <td align=right><div align=right>Qt 3.3.8</div> | 
|---|
| 147 | </table></div></address></body> | 
|---|
| 148 | </html> | 
|---|