Obtain the value from lineEdit to a new class



  • Hello someone can help me with this, i would like to take the value from a lineEdit box to a function in a new class.

    Already create the class using the wizard method of qe the name of the class is AG, and i have the follow files:

    ag.h

    #ifndef AG_H
    #define AG_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QSharedDataPointer>
    #include <QWidget>
    
    class AGData;
    
    class AG
    {
    public:
        AG();
        AG(const AG &);
        AG &operator=(const AG &);
        ~AG();
    
    private:
        QSharedDataPointer<AGData> data;
    };
    
    #endif // AG_H
    

    ag.cpp

    #include "ag.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <stdlib.h>
    #include <time.h>
    #include <cmath>
    
    class AGData : public QSharedData
    {
    public:
    
    };
    
    AG::AG() : data(new AGData)
    {
    
    }
    
    AG::AG(const AG &rhs) : data(rhs.data)
    {
    
    }
    
    AG &AG::operator=(const AG &rhs)
    {
        if (this != &rhs)
            data.operator=(rhs.data);
        return *this;
    }
    
    AG::~AG()
    {
    
    }
    

    In my mainwindow.h i have the element lineEdit. I have thinking about this and get this way of working, add my functions to the class with inputs parameters, but i don't like this way take a look:

    ag.h

    #ifndef AG_H
    #define AG_H
    
    #include <QMainWindow>
    #include <QObject>
    #include <QSharedDataPointer>
    #include <QWidget>
    
    class AGData;
    
    class AG
    {
    public:
        AG();
        AG(const AG &);
        AG &operator=(const AG &);
        ~AG();
        void initial_random_poblation(double lower, double upper, unsigned long int size);
        void fitness_line (unsigned long int size, double a, double b, bool min_max);
    
    
    private:
        QSharedDataPointer<AGData> data;
        unsigned long int i;
        double matrix[100000][4];
    };
    
    #endif // AG_H
    

    ag.cpp

    #include "ag.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <stdlib.h>
    #include <time.h>
    #include <cmath>
    
    class AGData : public QSharedData
    {
    public:
    
    };
    
    AG::AG() : data(new AGData)
    {
    
    }
    
    AG::AG(const AG &rhs) : data(rhs.data)
    {
    
    }
    
    AG &AG::operator=(const AG &rhs)
    {
        if (this != &rhs)
            data.operator=(rhs.data);
        return *this;
    }
    
    AG::~AG()
    {
    
    }
    
    void AG::initial_random_poblation(double lower, double upper, unsigned long int size){
    
        srand(time(NULL));
    
        for (i=0;i<size;i++){//desde i=0 hasta que i sea menor que el numero de inviduos, esto de debe a que el arreglo de los individuos de la poblacion comienza en cero
            matrix[i][0]=lower+drand48()*(upper-lower); //al dato que apunta ptr que es el arreglo de la poblacion se le agina un numero al azar entre el numero infierior y el superior
        }
    
    }
    
    void AG::fitness_line(unsigned long int size, double a, double b, bool min_max){
        for (i=0;i<size;i++){
            matrix[i][1]=1/(pow(matrix[i][0],2)+matrix[i][0]+1);
        }
    
    }
    
    

    then in the mainwindow.cpp i am doing the include of ag.h, then create a object of this class, and calling the functions with the parameters

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "ag.h"
    
    AG one;
    
           double a=ui->lineEdit->text().toDouble();
            double b=ui->lineEdit_2->text().toDouble();
            double upper=ui->lineEdit_5->text().toDouble();
            double lower=ui->lineEdit_6->text().toDouble();
            unsigned long int size=ui->lineEdit_9->text().toDouble();
    one.initial_random_poblation(lower,upper,size); //creation of initial poblation
    
    ......extra code
    

    but o don't like it, i fell this is not an elegant solution, please help could i use pointers in ag.cpp to the lineEdit box or something like that

    ag.cpp


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    A cleaner solution would be to use QDoubleSpinBox in place of your QLineEdit.

    Note that your AG class shouldn't know anything about your widgets. It's not its role.



  • @SGaist said in Obtain value from lineEDit to a new class:

    Hi and welcome to devnet,

    A cleaner solution would be to use QDoubleSpinBox in place of your QLineEdit.

    Note that your AG class shouldn't know anything about your widgets. It's not its role.

    Thanks for the welcome umm i take a look to the QDoubleSpinBox.

    Another question if i want to pass values of variables form the AG class to the mainwindow i have to declare this variables as public or protected sign is enough?


  • Lifetime Qt Champion

    Do you mean use an AG isntance in your MainWindow class ?



  • @SGaist said in Obtain the value from lineEdit to a new class:

    Do you mean use an AG isntance in your MainWindow class ?

    AG class will do mathematics equations and when it finish i have to return the results to show this in the mainwindow to the user, but in my class there are as a privates variables.

    If AG class is not related to any way to the class who use mainwindow, how can i return the variable to the mainwindow umm pointers?

    There is not special functions in qt to this?


  • Lifetime Qt Champion

    Currently you don't return anything from your AG class so it's kind of difficult to answer you.

    No, pointers are not the answer to everything.

    Looks like you're new to C++ and GUI programming, you should take the time to study the basics.


  • Qt Champions 2016

    @aurquiel
    Hi
    It would be better to use signals and slots
    https://forum.qt.io/topic/73448/access-method-from-class-object-in-another-class/8

    http://doc.qt.io/qt-5/signalsandslots.html

    So basically you let AG be a child of QObject and add Q_OBJECT macro.
    Then define a new signal like
    void calcReady( YourData );
    and a new slot in mainwindow :

    void MainWindow::calcReady( YourData data ) {
    .... use data ...
    }

    and hook up AG signal and calcReady with connect

    Then in AG when calc is finish
    you do

    emit calcReady(actual data);

    And mainwindow will know.


Log in to reply
 

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