|
楼主 |
发表于 2005-4-7 17:36:44
|
显示全部楼层
终于搞定了,在google找到了答案,贴上来share一下
我发现必须要把类分成.h和.cpp来写才能编译成功的,
不然会跳出一个undefined reference to `vtable for`的错误,
之前把程序想得复杂了,我修改了一下,其实实现起来是很简单的,
也许这个就是QT的机制吧
quit.h
[code:1]
#ifndef __QUIT_H
#define __QUIT_H
#endif
#include <qslider.h>
#include <qlcdnumber.h>
#include <qvbox.h>
class MyWidget : public QVBox
{
Q_OBJECT
public:
MyWidget( QWidget *parent=0, const char *name=0 );
int value() const
{
return slider->value();
}
public slots:
void setValue()
{
if (this->value() != 33)
//emit valueShow(33);
slider->setValue(33);
}
signals:
void valueShow(int);
protected:
QSlider *slider;
QLCDNumber *lcd;
};
[/code:1]
quit.cpp
[code:1]
#include <qapplication.h>
#include <qpushbutton.h>
#include <qfont.h>
#include "quit.h"
MyWidget::MyWidget( QWidget *parent, const char *name )
: QVBox( parent, name )
{
QPushButton *quit = new QPushButton( "Quit", this, "quit" );
quit->setFont( QFont( "Times", 18, QFont::Bold ) );
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) );
QPushButton *set = new QPushButton( "Set", this, "set" );
set->setFont( QFont( "Times", 18, QFont::Bold ) );
lcd = new QLCDNumber( 2, this, "lcd" );
lcd->setFixedHeight(30*2);
slider = new QSlider( Horizontal, this, "slider" );
slider->setRange( 0, 99 );
slider->setValue( 0 );
connect( slider, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)));
connect( set, SIGNAL(clicked()), this, SLOT(setValue()));
// connect( this, SIGNAL(valueShow(int)), slider,SIGNAL(setValue(int)) );
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
MyWidget w;
a.setMainWidget( &w );
w.show();
return a.exec();
}
[/code:1]
myqt.pro
[code:1]
######################################################################
# Automatically generated by qmake (1.07a) Wed Apr 6 00:22:21 2005
######################################################################
#TEMPLATE = app
#CONFIG -= moc
#INCLUDEPATH += .
# Input
#SOURCES += quit.cpp
TEMPLATE = app
TARGET = myqt
CONFIG += qt warn_on release
DEPENDPATH = ../../include
REQUIRES = full-config
HEADERS = quit.h
SOURCES = quit.cpp
[/code:1]
在Konsole中执行
#qmake -o Makefile myqt.pro
#make
OK 生成可执行文件,点击Set按钮就可以改变slider滑动条的数字了
我在这里设置是33
Enjoy~~~ |
|