我写了一个例子,这个例子只允许输入两个整数。
代码如下:
sumWidget.h文件
程序代码:
#ifndef SUMWIDGET_H
#define SUMWIDGET_H
#include <QtGui/QWidget>
class QLabel;
class QPushButton;
class SumWidget : public QWidget
{
Q_OBJECT
public:
SumWidget(QWidget *parent = 0);
public slots:
void add(int,int);
private:
QLabel *sumLabel;
QPushButton *okBtn;
};
#endif // SUMWIDGET_H
widget.h文件
程序代码:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
class QLabel;
class QLineEdit;
class QPushButton;
class SumWidget;
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
signals:
void sumSignal(int,int);
public slots:
void addSum();
void summandTextChanged(const QString &);
void addendTextChanged(const QString &);
void sumButtonIsOk();
private:
QLabel *summand;
QLabel *addend;
QLineEdit *summandLineEditText;
QLineEdit *addendLineEditText;
QPushButton *sum;
QString summanText;
QString addendText;
SumWidget *sumWidget;
};
#endif // WIDGET_H
sumWidget.cpp文件
程序代码:
#include <QtGui>
#include <QtDebug>
#include "sumWidget.h"
SumWidget::SumWidget(QWidget *parent)
:QWidget(parent)
{
sumLabel = new QLabel;
okBtn = new QPushButton(tr("OK"));
connect(okBtn,SIGNAL(clicked()),this,SLOT(close()));
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(sumLabel);
mainLayout->addWidget(okBtn);
setLayout(mainLayout);
}
void SumWidget::add(int summand, int addend)
{
int sum = summand + addend;
qDebug() << "sum:" << sum;
QString text = QString::number(sum);
qDebug() << "text:" << text;
sumLabel->setText(text);
}
widget.cpp文件
程序代码:
#include <QtGui>
#include <QtDebug>
#include "widget.h"
#include "sumWidget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
sumWidget = new SumWidget;
summand = new QLabel(tr("Summand:"));
addend = new QLabel(tr("Addend:"));
QRegExp reg = QRegExp("[0-9]{0,}");
summandLineEditText = new QLineEdit;
summandLineEditText->setValidator(new QRegExpValidator(reg,this));
connect(summandLineEditText,SIGNAL(textChanged(QString)),this,SLOT(summandTextChanged(QString)));
addendLineEditText = new QLineEdit;
addendLineEditText->setValidator(new QRegExpValidator(reg,this));
connect(addendLineEditText,SIGNAL(textChanged(QString)),this,SLOT(addendTextChanged(QString)));
QGridLayout *leftLayout = new QGridLayout;
leftLayout->addWidget(summand,0,0);
leftLayout->addWidget(summandLineEditText,0,1);
leftLayout->addWidget(addend,1,0);
leftLayout->addWidget(addendLineEditText,1,1);
sum = new QPushButton(tr("Sum"));
sum->setEnabled(false);
connect(sum,SIGNAL(clicked()),this,SLOT(addSum()));
connect(this,SIGNAL(sumSignal(int,int)),sumWidget,SLOT(add(int,int)));
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addWidget(sum);
setLayout(mainLayout);
}
Widget::~Widget()
{
}
void Widget::summandTextChanged(const QString &text)
{
summanText = text;
sumButtonIsOk();
}
void Widget::addendTextChanged(const QString &text)
{
addendText = text;
sumButtonIsOk();
}
void Widget::sumButtonIsOk()
{
if(!summanText.isEmpty() && !addendText.isEmpty())
sum->setEnabled(true);
else
sum->setEnabled(false);
}
void Widget::addSum()
{
int summand = summandLineEditText->text().toInt();
int addend = addendLineEditText->text().toInt();
qDebug() << "summand:" << summand;
qDebug() << "addend" << addend;
emit sumSignal(summand, addend);
sumWidget->show();
}
main.cpp文件
程序代码:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
例子很简单,自己仔细研究下吧。