如何把对话框中用户输入的信息在函数中应用
希望讲解的人给举个例子,不要光给个函数,本人刚学QT,举个例子说:比如说第一个界面出来两个输入框a,b,要求输入两个数字,点OK建之后弹出一个对话框得出a,b中输入数的和,再比如第一个界面出现若干个对话框,输入后点OK,讲对话框中内容依次存入一个数组内,并再弹出一个界面,显示数组中内容,也就是用户输入的信息,麻烦各位按照上面两个例子讲解一下,最好是拿出来完整的程序讲一下,多谢各位啦!
#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_Hwidget.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_HsumWidget.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(); }例子很简单,自己仔细研究下吧。