加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2013-06-03 15:11:45)
标签:

it

分类: Linux下C编程

转自:zgrjkflmkyc的专栏      

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

       C语言模块代码:

    

  1.   
  2.   
  3.   
  4. #include   
  5.   
  6. int main(int argc,char *argv[])  
  7.  
  8.    printf("Hello,I am program!\n");  
  9.    printf("now,I am invoked by 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.   
  2.   
  3. #ifndef MYTEST_H_  
  4. #define MYTEST_H_  
  5.   
  6. #include   
  7. #include   
  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.   
  2.   
  3. #include "MyTest.h"  
  4. #include   
  5. #include   
  6. #include   
  7. #include   
  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.   
  2.   
  3.   
  4. #include   
  5. #include   
  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.  

运行结果:

 

http://img.my.csdn.net/uploads/201301/20/1358660166_3257.png

(--------完--------)

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有