| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 643 人关注过本帖
标题:如何把对话框中用户输入的信息在函数中应用
只看楼主 加入收藏
黛玛
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-6
结帖率:0
收藏
已结贴  问题点数:10 回复次数:2 
如何把对话框中用户输入的信息在函数中应用
希望讲解的人给举个例子,不要光给个函数,本人刚学QT,举个例子说:比如说第一个界面出来两个输入框a,b,要求输入两个数字,点OK建之后弹出一个对话框得出a,b中输入数的和,再比如第一个界面出现若干个对话框,输入后点OK,讲对话框中内容依次存入一个数组内,并再弹出一个界面,显示数组中内容,也就是用户输入的信息,麻烦各位按照上面两个例子讲解一下,最好是拿出来完整的程序讲一下,多谢各位啦!
搜索更多相关主题的帖子: 信息 对话框 如何 用户 
2012-04-13 19:20
黛玛
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2011-11-6
收藏
得分:0 
有没有人理我呀?
2012-04-13 19:26
songhuirong1
Rank: 2
等 级:论坛游民
帖 子:116
专家分:38
注 册:2010-6-15
收藏
得分:10 
我写了一个例子,这个例子只允许输入两个整数。
代码如下:
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();
}
例子很简单,自己仔细研究下吧。

2012-04-15 22:57
快速回复:如何把对话框中用户输入的信息在函数中应用
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012544 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved