用 Qt实现三个窗口,一个label,两个button,
label显示数字,按第一个button使label中数字加1,按第二个button使label中数字复位为0.求代码。。。急...
label显示数字,按第一个button使label中数字加1, 按第二个button使label中数字复位为0.
求代码。。。急 展开
求代码。。。急 展开
1个回答
展开全部
简单的问题我解决,这为原创,转载请注明
------kingboy12345
///////widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QPushButton *clearNO,*addNO;
QLabel *label;
QHBoxLayout *qhboxlayout;
private slots:
void clearNOhandle();
void addNOhandle();
};
#endif // WIDGET_H
///////////widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
clearNO =new QPushButton("CLEAR");
addNO =new QPushButton("ADD");
label =new QLabel("0");
qhboxlayout =new QHBoxLayout(this);
qhboxlayout->addWidget(label);
qhboxlayout->addWidget(clearNO);
qhboxlayout->addWidget(addNO);
connect(clearNO,SIGNAL(clicked()),this,SLOT(clearNOhandle()));
connect(addNO,SIGNAL(clicked()),this,SLOT(addNOhandle()));
}
void Widget::addNOhandle(){
QString nostr;
int no;
nostr = label->text();
no = nostr.toInt();
no++;
nostr.setNum(no);
label->setText(nostr);
}
////////main函数
void Widget::clearNOhandle(){
label->setText("0");
}
Widget::~Widget()
{
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
------kingboy12345
///////widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QPushButton>
#include <QLabel>
#include <QHBoxLayout>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
QPushButton *clearNO,*addNO;
QLabel *label;
QHBoxLayout *qhboxlayout;
private slots:
void clearNOhandle();
void addNOhandle();
};
#endif // WIDGET_H
///////////widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
clearNO =new QPushButton("CLEAR");
addNO =new QPushButton("ADD");
label =new QLabel("0");
qhboxlayout =new QHBoxLayout(this);
qhboxlayout->addWidget(label);
qhboxlayout->addWidget(clearNO);
qhboxlayout->addWidget(addNO);
connect(clearNO,SIGNAL(clicked()),this,SLOT(clearNOhandle()));
connect(addNO,SIGNAL(clicked()),this,SLOT(addNOhandle()));
}
void Widget::addNOhandle(){
QString nostr;
int no;
nostr = label->text();
no = nostr.toInt();
no++;
nostr.setNum(no);
label->setText(nostr);
}
////////main函数
void Widget::clearNOhandle(){
label->setText("0");
}
Widget::~Widget()
{
}
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询