代码之家  ›  专栏  ›  技术社区  ›  SamuelNLP

存储qdialog中要在qmainwindow中使用的变量

  •  4
  • SamuelNLP  · 技术社区  · 14 年前

    我已经创建了一个对话框.h、dialog.cpp和dialog.ui,对话框中有qlineedit,以及“确定”和“取消”按钮,我想将这些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的值??

    这是我的对话

    #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
    

    这是我的主.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中的一些东西,主窗口

    2 回复  |  直到 14 年前
        1
  •  1
  •   Pacnos    14 年前

    我认为您可以在对话框中创建一个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();
    }
    
        2
  •  1
  •   BenMorel Manish Pradhan    12 年前

    尽管你可以申报 xMax 作为的公众成员 Dialog 只要使用它,更优雅和OO风格的方法就是编写一个在 public 您的区域 对话 类别:

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

    您还需要 setter 函数 planevolume 类,也在 平民的 面积,如:

    void setxMax(double xMax);
    

    该函数的主体将负责执行具有以下值的任何必要操作 x最大 。

    最后,在您的 main() 函数,您将执行以下操作:

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

    或者,您可以让 show() 的函数 平面体积 像

    planevolume::show(double xMax)
    {
      // Do whatever you want with XMax
    
      show();   // And call original show function
    }
    

    在这种情况下,您不需要 设置者 函数,然后调用

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