2012-08-30 21 views
4

我创建了dialog.h,dialog.cpp和dialog.ui,并且我在对话框中有qlineedit,以及ok和cancel按钮,并且我想要将这些linedit信息存储在不同文件的主窗口中。这是我的对话框代码。从qdialog存储变量在qmainwindow中使用

#include <QtGui/QApplication> 
#include "dialog.h" 
#include "ui_dialog.h" 

void Dialog::startplanevolume() 
{ 
    if (xMax==0) 
    { 
     ui->label_17->setText("Error: Can't start, invalid \nmeasures"); 
    } 
    else 
    { 
     this->accept();   
    } 
} 

// Define the length of the volume 
void Dialog::bmprange() 
{ 
// Getting some proprieties for the lenght of the volume 
QString XMAX=ui->lineEdit->text(); 
xMax=XMAX.toDouble(); 

if (xMax==0) 
{ 
    ui->label_17->setText("Error: invalid measures"); 
} 
else 
{ 
    ui->label_17->setText("Valid measures"); 
} 
} 

Dialog::Dialog(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Dialog) 
{ 
    ui->setupUi(this); 

    // Control volume measures 
    // Making the lineedit objects only accept numbers 
    ui->lineEdit->setValidator(new QIntValidator(this)); 
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(bmprange())); 


    // Start planevolume 
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startplanevolume())); 
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(hide())); 

} 

Dialog::~Dialog() 
{ 
    delete ui; 
} 

void Dialog::changeEvent(QEvent *e) 
{ 
    QDialog::changeEvent(e); 
    switch (e->type()) { 
    case QEvent::LanguageChange: 
     ui->retranslateUi(this); 
     break; 
    default: 
     break; 
    } 
} 

我怎样才能在mainwindow.cpp中使用xMax的值?

这里是我的dialog.h

#ifndef DIALOG_H 
#define DIALOG_H 

#include <QDialog> 

namespace Ui { 
    class Dialog; 
} 

class Dialog : public QDialog { 
    Q_OBJECT 
public: 
    Dialog(QWidget *parent = 0); 
    ~Dialog(); 

protected: 
    void changeEvent(QEvent *e); 

private: 
    Ui::Dialog *ui; 
    double xMax, yMax, zMax, xMMax, yMMax, zMMax, list[6]; 

public slots: 
    void bmprange(); 
    void startplanevolume(); 

}; 

#endif // DIALOG_H 

,这里是我的main.cpp

#include <QtGui/QApplication> 

#include "planevolume.h" 
#include "dialog.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 

    Dialog *dialog= new Dialog; 

    if (dialog->exec()) 
    { 
     planevolume mainwindow; 
     mainwindow.show(); 
     return app.exec(); 
    } 

return 0; 
} 

所以后来我想用XMAX来计算东西planevolume.cpp,在主窗口

+0

没有足够的信息。 'xMax'的定义方式和位置。你如何调用这个对话框?你何时何地创造它? – rpsml

+0

@rpsml我更新了它。 – SamuelNLP

回答

1

我认为你可以在对话框中创建一个getter函数。关闭对话框后,可以使用创建的getter函数访问变量。

返回的变量可以作为参数传递给mainwindow(planvolume.cpp)。

我希望有帮助。

编辑:

在dialog.h/dialog.cpp你添加一个功能:

double Dialog::getXMax() 
{ 
    return xMax; 
} 

之后,你可以存取权限的变量在main.cpp中:

Dialog *dialog= new Dialog; 

if (dialog->exec()) 
{ 
    double xMax = dialog->getXMax(); 
    planevolume mainwindow; 
    mainwindow.show(); 
    return app.exec(); 
} 
+0

你能举个例子,我觉得我不是很理解它。 – SamuelNLP

+0

像getproperty()? – SamuelNLP

+0

我已经编辑了我的帖子,我希望能帮助你更多 – Pacnos

1

尽管您可以将xMax声明为Dialog的公共成员,并且只是使用它,但更优雅和OO风格的方法是编写中声明的“getter”函数您Dialog类面积:

(...) 
public: 
    Dialog(QWidget *parent = 0); 
    ~Dialog(); 
    double getxMax() { return xMax; } 
(...) 

您还需要一个setter功能在你planevolume类,在public地区还宣布,为:

void setxMax(double xMax); 

此功能会照顾的身体做任何必要的事情,价值为xMax

最后,在main()功能,你会做以下几点:

if (dialog->exec()) 
{ 
    planevolume mainwindow; 
    mainwindow.setxMax(dialog->getxMax()); // This passes xMax to main window 
    mainwindow.show(); 
    return app.exec(); 
} 

或者,你可以只超载的planevolumeshow()功能

planevolume::show(double xMax) 
{ 
    // Do whatever you want with XMax 

    show(); // And call original show function 
} 

在这种情况下,你不需要setter功能,只是打电话

mainwindow.show(dialog->getxMax()); 
+0

我想我做错了什么。我创建了'void planevolume :: setxMax(double xMax) xp = xMax; }'但是现在我怎么在主窗口中使用xp? – SamuelNLP

+0

您的主窗口是'planevolume'的实例。从你的代码中,你可以猜测'xp'是在你的'planevolume'类中声明的。 'main()'函数中的mainwindow.setxMax()'行将'xMax'复制到'xp'中。所以你所要做的就是在适当的地方使用'xp'。 – rpsml

+0

我的xp值不对,它是像3.16e-310的东西,例如,当我的xMax是100 – SamuelNLP