2012-08-30 22 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