SOLVED: Storing variables from dialog and using it in a qvtkwidget



  • I've created a dialog where I ask for some lengths of a number of slices, as xmax, ymax, and zmax as the number of slices. I intend to use those numbers in the mainwindow in the qvtkwidget. I'll simplify and make the example to only one variable so you can understand and help me.

    Here's my dialog.cpp

    @#include <QtGui/QApplication>
    #include <QDir>
    #include <iostream>
    using namespace std;

    #include "dialog.h"
    #include "ui_dialog.h"

    // Create getters to transfer variables to main.cpp
    double Dialog::getxpax()
    {
    return xpax;
    }

    // Start the mainwindow
    void Dialog::startplanevolume()
    {
    // Getting some proprieties for the lenght of the volume
    QString XMAX=ui->lineEdit->text();
    xpax=XMAX.toDouble();

    if (xpax==0)
    {
        ui->label_17->setText("Error: Can't start, invalid \nmeasures");
        ui->label_17->setStyleSheet("QLabel { color : red; }");
    }
    else
    {
        this->accept();        
    }
    

    }

    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 QDoubleValidator(this));

    // Start planevolume
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(startplanevolume()));
    connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(hide()));

    }
    @

    The pushbutton is the ok button and the pushbutton_2 is the cancel button.

    In my mainwindow I created a setter function to set the value of the xmax.

    here's some code.

    @// Get stored data from dialog
    void planevolume::setxpax(double xpax)
    {
    xp=xpax;
    }
    @

    and when I use qDebug() the xp inside the settter shows me that the xp actually gets the xpax value.

    here's my main.cpp
    @
    #include <QtGui/QApplication>
    #include <iostream>
    using namespace std;

    #include "planevolume.h"
    #include "dialog.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    Dialog *dialog= new Dialog;
    
    if (dialog->exec&#40;&#41;&#41;
    {
        planevolume mainwindow;
        mainwindow.setxpax(dialog->getxpax());
        mainwindow.show();
        return app.exec&#40;&#41;;
    }
    

    return 0;
    }
    @

    So the only problem is that is here, at mainwindow as planevolume.cpp when I need it, the value has not been set,

    @planevolume::planevolume(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::planevolume)

    {
    ui->setupUi(this);

    // My vtk statements are here in the code, but they are
    // executed before the setter gives the value to my new
    // variable xp, so when I need the value it has not been set yet.
    @

    Any ideas guys?



  • Add xpax as a parameter to your planevolume class like so:
    @
    planevolume::planevolume(double xpax, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::planevolume),
    xp(xpax)

    {
    ui->setupUi(this);
    @
    ..and the line

    @
    if (dialog->exec())
    {
    planevolume mainwindow(dialog->getxpax());
    mainwindow.show();
    return app.exec();
    }
    @



  • thank you, well doing so I won't need the setter functions in my mainwindow right?



  • If you do not need to set xp afterwards, you can get rid of the setter function, yes.

    ...and to save you some pain in the future: make it a habit to initialize all member variables of your class in the constructor. This way the state of your class is well defined after construction.
    Hint:

    http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.