Qprocess Process Class-calling external programs

Source: Internet
Author: User

http://blog.csdn.net/newbee520/article/details/8279101starting a new process is very simple, just pass the program name and startup parameters you want to start to the start () function. For example: [CPP]View PlainCopy
    1. Qobject *parent;
    2. QString program = "Tar"
    3. Qstringlist arguments;
    4. Arguments << "CZVF" << "backup.tar.gz" << "Home";
    5. Qprocess *myprocess = new qprocess (parent);
    6. Qprocess->start (program, arguments);

when the start () function is called, the myprocess process immediately enters the startup state, but the TAR program has not been called and cannot read or write standard input.int ExitCode, qprocess::exitstatus exitstatus)
Error Constant Value descriptionQprocess::failedtostart 0 process failed to startqprocess::crashed 1 Process crashes after successful startupQprocess::timedout 2 Last call to waitfor ... () function timed out. The qprocess state does not change at this time and can be re-tuned with the function of the WaitFor () type Qprocess::writeerror 3 An error occurred while writing to the process. If the process has not started, or if the input channel is closedQprocess::readerror 4 An error occurred while reading data from the process. When the process has not startedqprocess::unknownerror 5 Unknown error. This is also the default value returned by the error () function. standard output of the process:stdout: Typically used for console-down outputstderr: Typically used for process printing errorsThey are essentially two separate streams of data.you can set the current read channel by calling the Setreadchanned () functionQT emits a readyread () signal when there is readable datareadyreadstandardoutput () signal is also emitted if the data is read in standard output and standard error channelsa readyreadstandarderror () signal is emitted if it is a standard errorThe readallstandardoutput () function reads data from the standard output channelThe Readallstandarderrot () function reads data from the standard error channelCall the Setreadchannelmode () function with the Mergedchannels parameter before the process starts to merge the standard output channel with the standard input error channel[CPP]View PlainCopy
  1. #include <QApplication>
  2. #include <QProcess>
  3. #include <QString>
  4. #include <iostream>
  5. int main (int argc, char *argv[])
  6. {
  7. Qapplication app (argc, argv);
  8. qprocess proc;
  9. Qstringlist arguments;
  10. Arguments << "-na";
  11. Proc.start ("netstat", arguments);
  12. //wait for process to start
  13. if (!proc.waitforstarted ())
  14. {
  15. Std::cout << "failed to start \ n";
  16. return false;
  17. }
  18. //Turn off the write channel because no data is written to the process, it is useless to
  19. Proc.closewritechannel ();
  20. //console output for saving the process
  21. Qbytearray Procoutput;
  22. //wait for process to end
  23. While (false = = proc.waitforfinished ())
  24. {
  25. Std::cout << "End failure \ n";
  26. return 1;
  27. }
  28. //Read the data from the process output to the console
  29. Procoutput = Proc.readall ();
  30. //output read Data
  31. Std::cout << procoutput.data () << Std::endl;
  32. //Return
  33. return exit_success;
  34. }
[CPP]View PlainCopy
    1. <pre name="code" class="cpp" ><pre></pre>
    2. <pre></pre>
    3. <pre></pre>
    4. <pre></pre>
    5. <pre></pre>
    6. <pre></pre>
    7. </pre>
in order to deepen the use of the Qprocess class, Next, we will paste the code of a case that you have written: This case is a command window under the simulation DOS. "widget.h" header file[CPP]View PlainCopy
  1. #ifndef Widget_h
  2. #define Widget_h
  3. #include <QProcess>
  4. #include <QWidget>
  5. Namespace Ui {
  6. class Widget;
  7. }
  8. Class Widget: Public qwidget
  9. {
  10. Q_object
  11. Public
  12. explicit Widget (Qwidget *parent = 0);
  13. ~widget ();
  14. Private
  15. Ui::widget *ui;
  16. Qprocess *pro; //Create a Process object
  17. QString out;
  18. Private Slots:
  19. void on_pushbutton_clicked (); //Run button slot
  20. void Readoutput (); //Read the data slot from the process
  21. };
  22. #endif//Widget_h

"widget.cpp" source file[CPP]View PlainCopy
  1. #include "Widget.h"
  2. #include "Ui_widget.h"
  3. Widget::widget (Qwidget *parent):
  4. Qwidget (parent),
  5. UI (new Ui::widget)
  6. {
  7. UI->SETUPUI (this);
  8. This->pro = new qprocess;
  9. this->setwindowflags (Qt::windowminimizebuttonhint);
  10. Qobject::connect (Ui->lineedit,signal (returnpressed ()),This,slot (on_pushbutton_clicked ()));
  11. //Ui->lineedit signal and slot connection, the cursor in the Ui->lineedit, press ENTER to achieve the same effect as the Click Run button
  12. Qobject::connect (Pro,signal (Readyread ()),This,slot (Readoutput ()));
  13. //The slot that triggers the output data when the data is ready to be read from the process
  14. }
  15. Widget::~widget ()
  16. {
  17. Delete UI;
  18. }
  19. void Widget::on_pushbutton_clicked ()
  20. {
  21. QString cmd = Ui->lineedit->text ();
  22. Pro->start (CMD);
  23. out = TR ("");
  24. Ui->textedit->settext (out);
  25. }
  26. void Widget::readoutput ()
  27. {
  28. Out + = Pro->readall ();
  29. Ui->textedit->settext (out);
  30. }

"main.cpp" source file
[CPP]View PlainCopy
  1. #include <QtGui/QApplication>
  2. #include "Widget.h"
  3. #include <QTextCodec>
  4. int main (int argc, char *argv[])
  5. {
  6. Qapplication A (argc, argv);
  7. Qtextcodec::setcodecforcstrings (Qtextcodec::codecforname ("GBK"));
  8. Qtextcodec::setcodecforlocale (Qtextcodec::codecforname ("GBK"));
  9. QTEXTCODEC::SETCODECFORTR (Qtextcodec::codecforname ("GBK"));
  10. Widget W;
  11. W.show ();
  12. return a.exec ();
  13. }
well, the interface for this case is that I dragged it out of QT designer, so there's no part of the code. Please use the following to view the source code.
Let's show you how it works:


Qprocess Process Class-calling external programs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.