Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SOLVED: Storing variables from dialog and using it in a qvtkwidget
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 3 Offline
    3 Offline
    32sthide
    wrote on 4 Sept 2012, 14:48 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cptG
      wrote on 4 Sept 2012, 15:00 last edited by
      #2

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

      1 Reply Last reply
      0
      • 3 Offline
        3 Offline
        32sthide
        wrote on 4 Sept 2012, 15:07 last edited by
        #3

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

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cptG
          wrote on 4 Sept 2012, 15:08 last edited by
          #4

          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/

          1 Reply Last reply
          0

          1/4

          4 Sept 2012, 14:48

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved