#ifndef SHOWPUSH_H
#define SHOWPUSH_H
#include<qlabel.h>
#include<qpushbutton.h>
#include<qvbox.h>
#include<qhbox.h>
class ShowPush:public QWidget
{
Q_OBJECT
public:
ShowPush(QWidget*parent=0,const char*name=0);
public slots:
//定义slot函数setlabelText(),常当指定连接的Signal发生时会呼叫此函数
void setlabelText();
private:
QVBox*vbox;
QLabel*label;
QPushButton*btn;
QHBox*hbox;
int i; //用于记录用户按了几次按钮
};
#endif
#include"showpush.h"
ShowPush::ShowPush(QWidget*parent,const char*name):QWidget(parent,name)
{
i=0;
vbox=new QVBox();
vbox->setAlignment(AlignCenter);
vbox->setGeometry(0,0,300,300);
label=new QLabel("Hello! Qt!",this,"label");
//label->setAlignment(AlignCenter);
label->setGeometry(45,20,250,30);
btn=new QPushButton("Push me",this,"btn");
btn->setGeometry(70,70,100,30);
hbox=new QHBox(vbox);
hbox->setGeometry(300,200,45,50);
connect(btn,SIGNAL(clicked()),SLOT(setlabelText()));
setFocusProxy(btn);
}
void ShowPush::setlabelText()
{
i=(i+1)%2;
if(i) label->setText("You push the button!");
else label->setText("Hello!Qt!");
}
#include <qapplication.h>
#include "showpush.h"
#include <qvbox.h>
int main(int argc,char**argv)
{
QApplication app(argc,argv);
//QVBox*vbox=new QVBox();
ShowPush wm;
wm.setGeometry(100,100,350,150);
app.setMainWidget(&wm);
wm.show();
return app.exec();
}
那按钮就不能垂直放呢?求解释