程序代码如下:
--------counter.h--------------------------------------
#include<QLabel>
#include<QWidget>
#include"stdio.h"
#include<QString>
#include<Qt>
class counter:public QWidget
{
Q_OBJECT
public:
counter();
public slots:
void IncCounter();
void DecCounter();
private:
int Counter;
QLabel *label;
};
----------------------counter.cpp---------------------------------
#include"counter.h"
counter::counter()
{
Counter=0;
label=new QLabel("0",this);
}
void counter::IncCounter()
{
char str[30];
sprintf(str,"%d",++Counter);
label->setText(str);
}
void counter::DecCounter()
{
char str[30];
sprintf(str,"%d",--Counter);
label->setText(str);
}
-----------------------------------------mainwindow.h-------------------------
#include<QPushButton>
#include"counter.h"
class MainWindow: public QWidget
{
Q_OBJECT
public :
MainWindow();
private:
QPushButton *AddButton;
QPushButton *DecButton;
counter *ct;
};
-------------------------------------------------mainwindow.cpp---------------------------
#include"mainwindow.h"
#include<QFont>
MainWindow::MainWindow()
{
QFont f("Helvetica",14,QFont::Bold);
setFont(f);
AddButton=new QPushButton("&Add",this);
AddButton->setGeometry(50,15,90,40);
DecButton=new QPushButton("&Dec",this);
DecButton->setGeometry(50,70,90,40);
ct=new counter();
ct->setGeometry(50,125,90,40);
connect(AddButton,SIGNAL(clicked()),ct,SLOT(counter::IncCounter()));
connect(DecButton,SIGNAL(clicked()),ct,SLOT(counter::DecCounter()));
}
------------------------------------------main.cpp-----------------------------
#include"mainwindow.h"
#include<QApplication>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
MainWindow mwindow;
mwindow.setGeometry(50,20,200,200);
mwindow.show();
return app.exec();
}
程序运行完毕后只有两个按钮会
显示。。。为什么会这样呢。。。谢谢啦~~~
[ 此贴被yunyun0220在2008-05-19 09:50重新编辑 ]