#include <qapplication.h>
#include <qpushbutton.h>
#include <qlineedit.h>
#include <qvalidator.h>
#include <qtooltip.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qwidget.h>
class SCalc : public QWidget
{
Q_OBJECT
public:
SCalc(QWidget *parent = 0,const char *name);
~SCalc();
private:
QPushButton * btn_calc;
QLabel * lb_title;
QLabel * lb_add;
QLabel * lb_equal;
QLineEdit * le_bjs;
QLineEdit * le_js;
QLineEdit * le_result;
}
SCalc::SCalc(QWidget *parent,const char *name)
: QWidget(parent,name)
{
setMaximumSize(QSize(400,80));
QToolTip:add(this,tr("copyright @ cprcf"));
QBoxLayout *vlayout = new QVBoxLayout(this,3);
lb_title->setText("A Simple Calculator");
vlayout->addWidget(lb_title);
lb_title->setAlignment(AlignHCenter | AlignTop);
lb_title->setFont(QFont("Times",20,QFont::Bold));
QBoxLayout *hlayout = new QHBoxLayout(vlayout);
le_bjs = new QLineEdit(this);
le_bjs->setMaxLength(10);
le_bjs->setValidator(new QDoubleValidator(le_bjs));
QToolTip::add(le_bjs,tr("Input Augend Here"));
hlayout->addWidget(le_bjs,5);
lb_add = new QLabel(this);
lb_add->setText("+");
hlayout->addWidget(lb_add,5);
le_js = new QLineEdit(this);
le_js->setMaxLength(10);
le_js->setValidator(new QDoubleValidator(le_js));
QToolTip::add(le_js,tr("Input Addend Here"));
hlayout->addWidget(le_js,5);
lb_equal = new QLabel(this);
lb_equal->setText("=");
hlayout->addWidget(lb_equal,5);
le_result = new QLineEdit(this);
le_result->setReadOnly(true);
hlayout->addWidget(le_result,5);
btn_calc = new QPushButton(this);
btn_calc->setText("calc");
QToolTip::add(btn,tr("click me"));
vlayout->addWidget(btn_calc);
vlayout->activate();
connect(btn_calc,SIGNAL(clicked()),le_result,SLOT());
}
SCalc::~SCalc()
{
}
int main(int argc,char **argv)
{
QApplication app(argc,argv);
SCalc *calc = new SCalc();
app.setMainWidget(calc);
calc->show();
return app.exec();
}
//下面是错误信息
本来开头没加Q_OBJECT 编译能通过 可以运行的
加了Q_OBJECT后 出现 如下错误提示
SCalc.o:In function 'SCalc::SCalc(QWidget *,char const *)':
SCalc.o(.text+0x29): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x48): undefined reference to 'SCalc::QPaintDevice virtual table'
SCalc.o(.text+0x7c): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x8c): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x29a): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x402): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o(.text+0x5c6): undefined reference to 'SCalc::tr(char const *,char const *)'
SCalc.o:In function 'SCalc::~SCalc(void)':
SCalc.o(.text+0x691): undefined reference to 'SCalc virtual table'
SCalc.o(.text+0x698): undefined reference to 'SCalc::QPaintDevice virtual table'