• 25918阅读
  • 13回复

【提问】qt如何调用c语言写的函数? [复制链接]

上一主题 下一主题
离线xd1198
 

只看楼主 倒序阅读 楼主  发表于: 2005-09-23
小弟是新手,刚开始qt编程。有两个问题:
1:现在有个c语言写的函数,我要在面板上通过一个按钮的按下,去调用这个函数,那么该如何去调用这个函数?
2:还需要根据这个函数的返回值去弹出对话框,根据返回值的不同来在对话框上显示不同的文字。那么这个函数的返回值如何保存?还是某个类的方法中去调用这个函数?是不是要通过信号(signal)和槽(slot)去实现?


刚开始学,而且由于任务比较急,也来不及深入看书,麻烦大家解答一下了。
[ 此贴被XChinux在2005-09-23 22:46重新编辑 ]
离线XChinux

只看该作者 1楼 发表于: 2005-09-23
Qt就是个C++类库,在C++中怎么使用C语言里的东西,在Qt中同样.
要先把Qt与C++,C++与C的关系搞清楚
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线zwcxxl

只看该作者 2楼 发表于: 2005-09-24
麻烦举个例子:)
离线acefunware

只看该作者 3楼 发表于: 2005-09-24
下面是引用zwcxxl于2005-09-24 11:52发表的:
麻烦举个例子:)

I think this is the first thing you want to do:
reading C++ and C book !
no body have so much time to do the example for you !
why not do it youself?
Boy^_^
email:acefunware@yahoo.com.cn
blog:http://acefunware.shineblog.com
离线XChinux

只看该作者 4楼 发表于: 2005-09-24
Qt的基础是C++,所以C++是必须要掌握的,另外,之所以经常把C和C++连起来称之为C/C++是因为他们两个的特殊关系,C++兼容C,所以尽管C++可以做为一个全新的语言来学习,但它的标准库都与C标准库兼容(不完全等同),所以学习C++就必须了解C,了解它们的关系,这样才能更好地利用C与发挥C++的效力
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线xd1198

只看该作者 5楼 发表于: 2005-09-25
首先多谢楼上各位了。主要是我没有说清楚。我就举例子说明吧 。比如我现在在面板上有个“登陆”按钮,而登陆的函数是c语言写的(这个不可能改变)。我要实现的功能就是当点击“登陆”按钮时,要调用这个登陆函数 ,并且,当这个函数返回不同的错误值时,要弹出对应的粗错误提示框。我的想法是这样的:例子如下
  1. #include <qapplication.h>
  2. #include <qpushbutton.h>
  3. #include <qfont.h>
  4. #include <stdio.h>
  5. class MyWidget : public QWidget
  6. {
  7. public:
  8.   MyWidget( QWidget *parent=0, const char *name=0 );
  9. public slots:
  10.         q_print(){printf("You add a function!\n");}
  11. };
  12. MyWidget::MyWidget( QWidget *parent, const char *name )
  13.     : QWidget( parent, name )
  14. {
  15.   setMinimumSize( 200, 120 );
  16.   setMaximumSize( 200, 120 );
  17.   QPushButton *quit = new QPushButton( "Quit", this, "quit" );
  18.   quit->setGeometry( 62, 40, 75, 30 );
  19.   quit->setFont( QFont( "Times", 18, QFont::Bold ) );
  20.   connect( quit, SIGNAL(clicked()), qApp, SLOT(q_print()/*quit()*/) );
  21. }
  22. int main( int argc, char **argv )
  23. {
  24.   QApplication a( argc, argv );
  25.   MyWidget w1;
  26.   w1.setGeometry( 50, 50, 100, 60 );
  27.   a.setMainWidget( &w1 );
  28.   w1.show();
  29.   w1.q_print();
  30.   return a.exec();
  31. }

这样,当点击按钮时,是不是就会调用q_print()函数i?我用的是qt-3.0.5-18,按照《24小时学通qt编程》书上说的,首先,对类声明使用moc:
$ moc ClassDeclaration.h -o ClassDeclaration.moc
然后,在cpp类文件(该文件具有函数定义)中包含moc文件:
#include<ClassDeclaration.moc>
最后,便以cpp文件,并将它与qt库链接:
$ g++ -lqt ClassDeclaration.cpp main.cpp -o MyProgram

但是,我在qt-3.0.5的include里没有找到ClassDeclaration.h,这个该如何链接了?
    主要是之前一直用的c,基本上没用过c++,下去后我会好好看c++的书的。再次多谢楼上几位了。
离线XChinux

只看该作者 6楼 发表于: 2005-09-25
你在源代码里connect的 q_print(),是属于那个类的,应该用this ,而不是用qApp(qApp是指向QApplication实例app的指针 ,它是没有q_print()这个成员的).
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线XChinux

只看该作者 7楼 发表于: 2005-09-25
C++必须要学,而且要基础打好,否则不可能用好Qt.
Qt里面有大量的静态成员函数,虚函数等等的东西.
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线xd1198

只看该作者 8楼 发表于: 2005-09-25
XChinux兄说的对。以前我曾看过C++的 书,另外还学过java的课程,所以对虚函数、静态成员函数、继承等有一定的了解,我会接着再好好看书的。多谢了。
有个问题就是:
用了slot后,该如何设置了?就是关于ClassDeclaration.moc的问题。

"用了slot后,该如何设置了?就是关于ClassDeclaration.moc的问题" 已经解决 了,多谢XChinux兄了,看了你的帖子:《KDE2/Qt编程金典》第二章:创建并显示一个窗口——2.6 为一个信号定义slot 等几个帖子,受益匪浅。其实,只要在类的定义中加上宏Q_OBJECT,例如:
8 class MainWidget: public QWidget
9 {
10 Q_OBJECT
11 public:
12 MainWidget(QWidget *parent=0,const char *name=0);
13 private slots:
14 void popupEnterName();
15 void changeCaption(QString &);
16 };

原贴地址:http://www.qtcn.org/bbs/read.php?tid=1238

再次多谢XChinux兄了。不知道这本《KDE2/Qt编程金典》在北京哪儿有卖的?网上书店好像都没有。如果没有卖的,电子版哪里有啊?
[ 此贴被xd1198在2005-09-26 10:41重新编辑 ]
离线lophyxp

只看该作者 9楼 发表于: 2005-10-06
猜测:直接调用应该就可以。
下面是从《GCC:the Complete Reference》中摘出来的。
Calling C from C++
The following example is a C++ program that calls a C function named csayhello().
This call can be made directly because the function is declared in the C++ program as
extern "C":

/* cpp2c.cpp */
#include <iostream>
extern "C" void csayhello(char *str);
int main(int argc,char *argv[])
{
csayhello("Hello from cpp to c");
return(0);
}
The C function requires no special declaration and appears as follows:
/* csayhello.c */
#include <stdio.h>
void csayhello(char *str)
{
printf("%s\n",str);
}
The following three commands compile the two programs and link them into an
executable. The flexibility of g++ and gcc allow this to be done in different ways, but
this set of commands is probably the most straightforward:
$ g++ -c cpp2c.cpp -o cpp2c.o
$ gcc -c csayhello.c -o csayhello.o
$ gcc cpp2c.o csayhello.o -lstdc++ -o cpp2c
Notice that it is necessary to specify the standard C++ library in the final link because
the gcc command is used to invoke the linker instead of the g++ command. If g++ had
been used, the C++ library would have been implied.
It is most common to have the function declarations in a header file and to have the
entire contents of the header file included as the extern "C" declaration. The syntax
for this is standard C++ and looks like the following:
extern "C" {
int mlimitav(int lowend, int highend);
void updatedesc(char *newdesc);
double getpct(char *name);
};
离线lophyxp

只看该作者 10楼 发表于: 2005-10-06
对于库函数,应该是直接调用。

对于自定义函数,可能需要改一下头文件。
在所有宏定义和函数说明之加入:
#ifdef __cplusplus
extern "C" {
#endif

在所有宏定义和函数说明之加入:
#ifdef __cplusplus
}
#endif


例如:以下是我自己写得一个头文件
typedef struct{
    int year;
    int month;
    int day;
}date;

int GetWeekNo(date);

修改后就变成
#ifdef __cplusplus
extern "C" {
#endif

typedef struct{
    int year;
    int month;
    int day;
}date;

int GetWeekNo(date);
#ifdef __cplusplus
}
#endif


注意大小写,以及cplusplus前面是两条下划线。
离线XChinux

只看该作者 11楼 发表于: 2005-10-06
楼上地说得好详细啊,嗬嗬。
to 搂主:关于<<KDE2/Qt编程宝典>>的书,我手头有这本书,不过是好几年前买的,不知道现在有没有了,电子版的,只找到了英文版的。在论坛的ftp站点上有。论坛的ftp站点地址,你可以在论坛公告栏区看到。
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线xd1198

只看该作者 12楼 发表于: 2005-10-08
多谢楼上各位兄弟了!关于qt的书现在都买不到。这段时间都是在看reference。
离线acefunware

只看该作者 13楼 发表于: 2005-10-09
c++中直接可以用c语言的
但是要注意c++和c的库之间的连接
email:acefunware@yahoo.com.cn
blog:http://acefunware.shineblog.com
快速回复
限100 字节
 
上一个 下一个