4

I've created a dialog.h, dialog.cpp and a dialog.ui, and I have qlineedit in the dialog, as well as ok and cancel button, and i want to store those linedit information to use in a mainwindow in a different file. here's my dialog code.

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

how can i get to use the value of xMax in mainwindow.cpp??

here's my 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

here's my 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;
}

so then I want to use xMax to calculate something in planevolume.cpp, the mainwindow

SamuelNLP
  • 4,038
  • 9
  • 59
  • 102
  • There is not enough information. How and where `xMax` is defined. How are you calling this dialog? When and where are you creating it? – rpsml Aug 30 '12 at 10:54

2 Answers2

1

I think you can create a getter function in the dialog. After the dialog is closed, you can access the variable using the created getter function.

The returned variable you can give as a parameter to the mainwindow (planvolume.cpp).

I hope that helps.

Edit:

In the dialog.h / dialog.cpp you add an function:

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

After that you can acces the variable in the main.cpp:

Dialog *dialog= new Dialog;

if (dialog->exec())
{
    double xMax = dialog->getXMax();
    planevolume mainwindow;
    mainwindow.show();
    return app.exec();
}
Pacnos
  • 194
  • 16
  • can you give me an example, i think i'm not understanding it quite right. – SamuelNLP Aug 30 '12 at 11:33
  • 1
    no this is not possible. Void means that there is NO return, so you can't using the return statement. – Pacnos Aug 30 '12 at 16:13
  • I don't think this will work. Samuel can you try it and tell us? – UmNyobe Aug 30 '12 at 16:14
  • UmNyobe, you're right, I have used void as return value, I have chaged it to double, sry for this mistake ... – Pacnos Aug 30 '12 at 16:16
  • you say I can aces the variable in main.cpp, but I need it in plainvolume. – SamuelNLP Aug 30 '12 at 16:19
  • After getting the Variable in the main.cpp (sse line double xMax = dialog->get..) you have the variable in the main.cpp Now you can write a set methode in the planevolume and call the set methode bevore calling the mainwindow.show() function. – Pacnos Aug 30 '12 at 16:22
  • @Pacnos well doing so, if I put a xMax as 512 I get a xMax in planevolume of 2.14748e+09 and I don't know what's wrong – SamuelNLP Aug 31 '12 at 08:54
  • @SamuelNLP Have you checked the value in getXMax function? Has the variable xMax the correct value in the Dialog class? Please check this, using the qDebug() function ... – Pacnos Aug 31 '12 at 16:35
  • @Pacnos In the main.cpp they are fine, but in the planevolume.cpp they have theses values. – SamuelNLP Sep 03 '12 at 09:37
  • @SamuelNLP could you post your code from the main.cpp and planevolume.cpp please – Pacnos Sep 08 '12 at 10:09
1

Although you could declare xMax as public member of Dialog and just use it, the more elegant and OO style approach is to write a "getter" function declared in the public area of your Dialog class:

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

You would also need a setter function in your planevolume class, also declared in the public area, as:

void setxMax(double xMax);

The body of this function would take care of doing whatever is necessary with the value of xMax.

Finally, in your main() function, you would do the following:

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

Alternatively, you could just overload the show() function of planevolume as

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

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

in which case you do not need the setter function and just call

mainwindow.show(dialog->getxMax());
BenMorel
  • 34,448
  • 50
  • 182
  • 322
rpsml
  • 1,486
  • 14
  • 21
  • i guess i'm doing something wrong. I created `void planevolume::setxMax(double xMax) { xp=xMax; }`but now how can I use xp in the mainwindow? – SamuelNLP Aug 30 '12 at 15:32
  • Your main window is an instantiation of `planevolume`. From your code, one can guess that `xp` was declared in your `planevolume` class. The line `mainwindow.setxMax()` in your `main()` function copies `xMax` into `xp`. So all you have to do is to use `xp` wherever it is appropriate. – rpsml Aug 30 '12 at 19:40
  • my xp value is not right, it is something like 3.16e-310 when for example my xMax is 100 – SamuelNLP Sep 03 '12 at 10:54