//calcu.h
#ifndef CALCU_H
#define CALCU_H
#include <QMainWindow>
namespace Ui {
class calcu;
}
class calcu : public QMainWindow
{
Q_OBJECT
public:
explicit calcu(QWidget *parent = 0);
~calcu();
private:
Ui::calcu *ui;
private slots:
void labelValueChange(int);
};
#endif // CALCU_H
//calcu.cpp
#include "calcu.h"
#include "ui_calcu.h"
calcu::calcu(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::calcu)
{
ui->setupUi(this);
connect(ui->spinBox,SIGNAL(valueChanged(int)),this,SLOT(labelValueChange(int)));
connect(ui->spinBox_2,SIGNAL(valueChanged(int)),this,SLOT(labelValueChange(int)));
}
calcu::~calcu()
{
delete ui;
}
void calcu::labelValueChange(int v)
{
ui->label_4->setNum(ui->spinBox->value()+ui->spinBox_2->value());
}
//main.cpp
#include "calcu.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
calcu w;
w.show();
return a.exec();
}
以上ui亦可以使用uiloader动态载入ui文件
//calcu.h
#ifndef CALCU_H
#define CALCU_H
#include <QMainWindow>
#include <QSpinBox>
#include <QLabel>
class calcu : public QMainWindow
{
Q_OBJECT
public:
explicit calcu(QWidget *parent = 0);
~calcu();
private:
QWidget *w;
QSpinBox *spinbox;
QSpinBox *spinbox_2;
QLabel *label_4;
private slots:
void labelValueChange(int);
};
#endif // CALCU_H
//calcu.cpp
#include "calcu.h"
#include <QtUiTools>
#include <QUiLoader>
#include <QFile>
calcu::calcu(QWidget *parent) :
QMainWindow(parent)
{
QUiLoader loader;
QFile file(":/calcu.ui");
file.open(QFile::ReadOnly);
w=loader.load(&file, this);
spinbox=findChild<QSpinBox*>("spinBox");
spinbox_2=findChild<QSpinBox*>("spinBox_2");
label_4=findChild<QLabel *>("label_4");
w->setWindowFlags(Qt::Widget);
connect(spinbox,SIGNAL(valueChanged(int)),this,SLOT(labelValueChange(int)));
connect(spinbox_2,SIGNAL(valueChanged(int)),this,SLOT(labelValueChange(int)));
setCentralWidget(w);
}
calcu::~calcu()
{
}
void calcu::labelValueChange(int v)
{
label_4->setNum(spinbox->value()+spinbox_2->value());
}
//main.cpp
#include "calcu.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
calcu w;
w.show();
return a.exec();
}
效果图
世界清静了!