Qt for beginners-calling external C language modules in QT

Source: Internet
Author: User

Call the compiled C language module, PASS Parameters, and print the returned results.

C language module code:

/********** Test. c ********** // *********** after compiling with GCC, the executable file test is produced and stored in .... */# include <stdio. h> int main (INT argc, char * argv []) {printf ("Hello, I am a C program! \ N "); printf (" now, I am invoked by a program called QT! \ Nfollowing are the parameters that QT sends to me: \ n "); printf (" % s \ n ", argv [0]); printf (" % s \ n ", argv [1]); printf ("% s \ n", argv [2]); Return 0 ;}

Qt code:

 

/********MyTest.h***********/#ifndef MYTEST_H_#define MYTEST_H_#include <QtGui/QWidget>#include <QtGui/QPushButton>class MyTest : public QWidget{  Q_OBJECT  public:  MyTest();  ~MyTest();  public slots:  void invokeC();  private:  QPushButton *pb;};#endif

/******* Mytest. CPP **********/# include "mytest. H "# include <qtcore/qprocess> # include <iostream> # include <qtcore/qtextstream> # include <qtcore/qiodevice> mytest: mytest (): qwidget () {This-> setgeometry (, 50); Pb = new qpushbutton ("click to call C program", this); Pb-> setgeometry (, 50); Connect (Pb, signal (clicked (), this, slot (invokec ();} mytest ::~ Mytest () {} void mytest: invokec () {qprocess * process = new qprocess (); qstringlist STR; Str. clear (); STR <"A" <"B"; process-> Start (".. /C/test ", STR); process-> waitforstarted (); process-> waitforfinished (); qbytearray QB = process-> readall (); qstring str22 (QB ); qtextstream cout (stdout); cout <str22 <Endl ;}

/*********** Main. CPP ************ // ********* after compilation, put it in ..... */# include <qtgui/qapplication> # include <qtcore/qtextcodec> # include "mytest. H "int main (INT argc, char * argv []) {qapplication A (argc, argv); qtextcodec * codec = qtextcodec: codecforlocale (); qtextcodec :: setcodecforcstrings (codec);. setfont (qapplication: font (); mytest * Mt = new mytest; MT-> show (); Return a.exe C ();}

Running result:

(-------- End --------)

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.