初学Qt之--在Qt中调用外部C语言模块

原地址:blog.csdn.net/zgrjkflmkyc/article/details/8521953

版权声明:本文为博主原创文章,未经博主允许不得转载。

      调用外部已编译好的C语言模块,传递参数并将结果返回打印出来。

       C语言模块代码:

    

  1. /**********Test.c**********/  
  2. /**********用gcc编译后生产那个的可执行文件test放在..../invokec/C/目录下**********/  
  3.   
  4. #include <stdio.h>  
  5.   
  6. int main(int argc,char *argv[])  
  7. {  
  8.    printf("Hello,I am a C program!\n");  
  9.    printf("now,I am invoked by a program called Qt!\nfollowing are the parameters that Qt sends to me:\n");  
  10.    printf("%s\n",argv[0]);  
  11.    printf("%s\n",argv[1]);  
  12.    printf("%s\n",argv[2]);  
  13.    return 0;  
  14. }  

  Qt代码:

 

  1. /********MyTest.h***********/  
  2.   
  3. #ifndef MYTEST_H_  
  4. #define MYTEST_H_  
  5.   
  6. #include <QtGui/QWidget>  
  7. #include <QtGui/QPushButton>  
  8.   
  9. class MyTest : public QWidget  
  10. {  
  11.   Q_OBJECT  
  12.   public:  
  13.   MyTest();  
  14.   ~MyTest();  
  15.   public slots:  
  16.   void invokeC();  
  17.   private:  
  18.   QPushButton *pb;  
  19. };  
  20.   
  21. #endif  

  1. /********MyTest.cpp**********/  
  2.   
  3. #include "MyTest.h"  
  4. #include <QtCore/QProcess>  
  5. #include <iostream>  
  6. #include <QtCore/QTextStream>  
  7. #include <QtCore/QIODevice>  
  8.   
  9. MyTest::MyTest()  
  10. :QWidget()  
  11. {  
  12.   this->setGeometry(0,0,200,50);  
  13.   pb=new QPushButton("点击调用C程序",this);  
  14.   pb->setGeometry(0,0,200,50);  
  15.   connect(pb,SIGNAL(clicked()),this,SLOT(invokeC()));  
  16. }  
  17.   
  18. MyTest::~MyTest()  
  19. {  
  20. }  
  21.   
  22. void MyTest::invokeC()  
  23. {  
  24.    QProcess *process=new QProcess();  
  25.    QStringList str;  
  26.    str.clear();  
  27.    str << "a" << "b" ;  
  28.    process->start("../C/test",str);   
  29.    process->waitForStarted();  
  30.    process->waitForFinished();  
  31.    QByteArray qb=process->readAll();  
  32.    QString str22(qb);  
  33.    QTextStream cout(stdout);  
  34.    cout<<str22<<endl;  
  35. }  

  1. /***********Main.cpp************/  
  2. /*******编译后放在...../invokeC/Qt/目录下************/  
  3.   
  4. #include <QtGui/QApplication>  
  5. #include <QtCore/QTextCodec>  
  6. #include "MyTest.h"  
  7.   
  8. int main(int argc,char *argv[])  
  9. {  
  10.    QApplication a(argc,argv);  
  11.    QTextCodec *codec = QTextCodec::codecForLocale();  
  12.    QTextCodec::setCodecForCStrings(codec);  
  13.    a.setFont(QApplication::font());  
  14.      
  15.    MyTest *mt=new MyTest;  
  16.    mt->show();  
  17.    return a.exec();  
  18. }  

运行结果:

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值