将QDialog中的变量存储以在QMainWindow中使用

4

我已经创建了dialog.h、dialog.cpp和dialog.ui文件,并在对话框中添加了qlineedit,以及ok和cancel按钮。我想将这些linedit信息存储起来,在不同的文件中的mainwindow中使用。以下是我的对话框代码。

#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;
}

那么我想在mainwindow中使用xMax来计算planevolume.cpp中的某些内容。


没有足够的信息。xMax 是如何定义的,它在哪里定义的?你是如何调用这个对话框的?你是在什么时候和地方创建它的? - rpsml
2个回答

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();
}

你能给我一个例子吗?我觉得我还没有完全理解它。 - SamuelNLP
1
不,这是不可能的。 Void表示没有返回值,所以你不能使用return语句。 - Pacnos
我觉得这不会起作用。Samuel,你能试试并告诉我们吗? - UmNyobe
UmNyobe,你是对的,我之前使用了void作为返回值,我已经将其更改为double,对于这个错误我很抱歉... - Pacnos
你说我可以在main.cpp中访问变量,但我需要它在plainvolume中。 - SamuelNLP
显示剩余5条评论

1

虽然你可以将xMax声明为Dialog的公共成员并直接使用它,但更优雅和面向对象的方法是编写一个在Dialog类的public区域中声明的“getter”函数:

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

您的planevolume类还需要一个setter函数,也要在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());

我猜我做错了什么。我创建了void planevolume::setxMax(double xMax) { xp=xMax; }但现在我该如何在mainwindow中使用xp? - SamuelNLP
你的主窗口是 planevolume 的一个实例。从你的代码可以猜测出 xp 是在 planevolume 类中声明的。mainwindow.setxMax() 这一行在 main() 函数中将 xMax 复制到了 xp 中。因此,你所要做的就是在适当的地方使用 xp - rpsml
我的经验值不正确,例如当我的xMax为100时,它是类似于3.16e-310的数值。 - SamuelNLP

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接