#include <qapplication.h>
#include <qwidget.h>
#include <qslider.h>
#include <qprogressbar.h>
#include <qpushbutton.h>
class MyMainWindow : public QWidget
{
Q_OBJECT
public:
MyMainWindow();
public slots:
void set_step();
private:
QProgressBar *bar;
// QSlider *slider;
QPushButton *add;
QPushButton *dec;
};
MyMainWindow::MyMainWindow()
{
setGeometry(100, 100, 200, 90);
bar = new QProgressBar(100, this);
bar->setGeometry(10, 10, 180, 30);
add = new QPushButton("+", this);
add->setGeometry(10, 50, 10, 10);
dec = new QPushButton("-", this);
dec->setGeometry(50, 50, 10, 10);
// slider = new QSlider(0, 100, 10, 0, Horizontal, this);
// slider->setGeometry(10, 50, 180, 30);
connect(add, SIGNAL(clicked()), this , SLOT(set_step()));
connect(dec, SIGNAL(clicked()), this , SLOT(set_step()));
}
void MyMainWindow::set_step()
{
printf("haha");
}
int main(int argc, char **argv)
{
QApplication a(argc, argv);
MyMainWindow w;
a.setMainWidget(&w);
w.show();
a.exec();
}
编译报出:
progress.o: In function `MyMainWindow::MyMainWindow()':
progress.cpp:(.text+0x1dc): undefined reference to `vtable for MyMainWindow'
progress.o: In function `main':
progress.cpp:(.text+0x268): undefined reference to `vtable for MyMainWindow'
progress.o: In function `MyMainWindow::MyMainWindow()':
progress.cpp:(.text+0x43c): undefined reference to `vtable for MyMainWindow'
我的解决办法,把类定义包含到头文件中去
原因:qmake不会处理.cpp文件里的Q_OBJECT,所以,如果在.cpp文件中有它的话,也会产生undefined reference to vtable for "xxx::xxx". 这时,需要先用moc xxxx.cpp生成相应的moc文件,再包含到.cpp里面去,才能解决这个问题.
引用来源:http://blog.donews.com/netexe/archive/2006/02/09/720544.aspx