4

我创建了一个 dialog.h、dialog.cpp 和一个 dialog.ui,我在对话框中有 qlineedit,还有 ok 和 cancel 按钮,我想存储这些 linedit 信息以在不同的主窗口中使用文件。这是我的对话代码。

#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 的值?

这是我的对话.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 中计算一些东西

4

2 回答 2

1

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

您可以将返回的变量作为参数提供给主窗口 (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();
}
于 2012-08-30T11:27:05.547 回答
1

虽然您可以声明xMax为公共成员并直接使用它,但更优雅和 OO 风格的方法是编写一个在您的类区域中Dialog声明的“getter”函数:publicDialog

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

show()或者,您可以重载planevolumeas

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

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

在这种情况下,您不需要该setter功能,只需调用

mainwindow.show(dialog->getxMax());
于 2012-08-30T12:19:04.410 回答