初学Qt之--在Qt中调用外部C语言模块
标签:
it |
分类: Linux下C编程 |
调用外部已编译好的C语言模块,传递参数并将结果返回打印出来。
[cpp] view
plaincopy
-
-
-
-
#include
-
-
int
main( intargc, 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; -
}
[cpp] view
plaincopy
-
-
-
#ifndef
MYTEST_H_ -
#define
MYTEST_H_ -
-
#include
-
#include
-
-
class
MyTest public: QWidget -
{
-
Q_OBJECT -
public: -
MyTest(); -
~MyTest(); -
public slots: -
void invokeC(); -
private: -
QPushButton *pb; -
};
-
-
#endif
[cpp] view
plaincopy
-
-
-
#include
"MyTest.h" -
#include
-
#include
-
#include
-
#include
-
-
MyTest::MyTest()
-
:QWidget()
-
{
-
this->setGeometry(0,0,200,50); -
pb=new QPushButton( "点击调用C程序",this); -
pb->setGeometry(0,0,200,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; -
}
[cpp] view
plaincopy
-
-
-
-
#include
-
#include
-
#include
"MyTest.h" -
-
int
main( intargc, char*argv[]) -
{
-
QApplication a(argc,argv); -
QTextCodec *codec = QTextCodec::codecForLocale(); -
QTextCodec::setCodecForCStrings(codec); -
a.setFont(QApplication::font()); -
-
MyTest *mt=new MyTest; -
mt->show(); -
return a.exec(); -
}
运行结果:
http://img.my.csdn.net/uploads/201301/20/1358660166_3257.png
(--------完--------)
前一篇:[转载]libcap库函数介绍

加载中…