首先多谢楼上各位了。主要是我没有说清楚。我就举例子说明吧 。比如我现在在面板上有个“登陆”按钮,而登陆的函数是c语言写的(这个不可能改变)。我要实现的功能就是当点击“登陆”按钮时,要调用这个登陆函数 ,并且,当这个函数返回不同的错误值时,要弹出对应的粗错误提示框。我的想法是这样的:例子如下
- #include <qapplication.h>
- #include <qpushbutton.h>
- #include <qfont.h>
- #include <stdio.h>
- class MyWidget : public QWidget
- {
- public:
- MyWidget( QWidget *parent=0, const char *name=0 );
- public slots:
- q_print(){printf("You add a function!\n");}
- };
- MyWidget::MyWidget( QWidget *parent, const char *name )
- : QWidget( parent, name )
- {
- setMinimumSize( 200, 120 );
- setMaximumSize( 200, 120 );
- QPushButton *quit = new QPushButton( "Quit", this, "quit" );
- quit->setGeometry( 62, 40, 75, 30 );
- quit->setFont( QFont( "Times", 18, QFont::Bold ) );
- connect( quit, SIGNAL(clicked()), qApp, SLOT(q_print()/*quit()*/) );
- }
- int main( int argc, char **argv )
- {
- QApplication a( argc, argv );
- MyWidget w1;
- w1.setGeometry( 50, 50, 100, 60 );
- a.setMainWidget( &w1 );
- w1.show();
- w1.q_print();
- return a.exec();
- }
这样,当点击按钮时,是不是就会调用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++的书的。再次多谢楼上几位了。