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. Obtain the value from lineEdit to a new class

Obtain the value from lineEdit to a new class

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.9k Views 2 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.
  • A Offline
    A Offline
    aurquiel
    wrote on last edited by aurquiel
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        A Offline
        A Offline
        aurquiel
        wrote on last edited by aurquiel
        #3

        @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?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • SGaistS SGaist

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

            A Offline
            A Offline
            aurquiel
            wrote on last edited by aurquiel
            #5

            @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?

            mrjjM 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • A aurquiel

                @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?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by mrjj
                #7

                @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.

                1 Reply Last reply
                2

                • Login

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