用 Qt实现三个窗口,一个label,两个button,

2024-11-29 10:52:15
推荐回答(1个)
回答(1):

简单的问题我解决,这为原创,转载请注明
------kingboy12345

///////widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include
#include
#include
#include
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
#include "widget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}