QT编程如何在一个qmainwindow窗口顶部加一个label呀,就是跟窗口标题在一行的

2024-11-29 09:01:58
推荐回答(1个)
回答1:

#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();
}