Search code examples
c++qtnetbeansqlineedit

Can't retrieve text value from QLineEdit


Im trying to retrieve the text() value from a simple QLineEdit function but I'm unable to make it work, I'm new to Qt so I'm kinda lost, especially when using pointers.

Inside my ui_Ventas2.h file, Qt already initialize all the classes

QLineEdit *lineClienteNo;

but when I want to retrieve the text i do something like this, on my main.cpp file

QLineEdit *ClienteNo;
ClienteNo->lineClienteNo.text();

I'm doing something very basic, yet I can't link it, here's my full code:

#include <QApplication>
#include "Ventas2.h"
#include "ui_Ventas2.h"

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

QApplication app(argc, argv);
Ventas2 VentanaPrincipal;
VentanaPrincipal.show();

QLineEdit *ClienteNo;
ClienteNo->lineClienteNo.text();

return app.exec();
}

Should I make a new class on my Ventas2.h header file to link the created class from Qt to make it work or something??

Note: I'm using Netbeans 8.1 and the Qt Framework 5.7 on Mac


Solution

  • Ok meabe theres a mistake on my first question, the class that im trying to access comes from the Qt generated header file called, ui_Ventas.h, but the general header file is called Ventas2.h and it has this code

    #ifndef _VENTAS2_H
    #define _VENTAS2_H
    
    #include "ui_Ventas2.h"
    
    class Ventas2 : public QMainWindow {
    Q_OBJECT
       public:
         Ventas2();
         virtual ~Ventas2();
       private:
    Ui::Ventas2 widget;
    };
    
    #endif /* _VENTAS2_H */
    

    but im trying to get the text from ui_Ventas2.h, the QLineEdit class call LineClienteNo

    #ifndef UI_VENTAS2_H
    #define UI_VENTAS2_H
    
    ....
    #include <QtWidgets/QLineEdit>
    ....
    
    class Ui_Ventas2
    {
    public:
       QLineEdit *lineClienteNo;
       QWidget *centralwidget;
       QPushButton *pushButtonOk;
       QLabel *label_Cliente;
       QWidget *widget;
    

    i use something like you had in your post and it finds what i need

    QString text = Ui_Ventas2.lineClienteNo->text();
    

    and i get the next error

    main.cpp:26:20: error: 'Ui_Ventas2' does not refer to a value
       QString text = Ui_Ventas2.lineClienteNo->text();
                   ^
    ./ui_Ventas2.h:30:7: note: declared here
        class Ui_Ventas2
    

    I think i got it, but i dont understand why if i find the class that i need, it keeps giving me errors, already checking tutorials about it

    Note: my mistake, i needed to declare in the Ventas2.h header file first the public slots

    public slots:
      void textChanged(const QString& text);
    

    then declare on Ventas2.cpp.cc file the function

    void Ventas2::textChanged(const QString& text) 
     {
    if (0 < text.trimmed().length())
      { widget.lineEditMostrar->setText(text.trimmed());
      }
    else {
       widget.lineEditMostrar->clear();
      }
    }
    

    Then, use the connect Qt Function to join all together

    Ventas2::Ventas2() {
      widget.setupUi(this);
    
      connect(widget.lineClienteNo,SIGNAL(textChanged(const QString&)),this,SLOT(textChanged(const QString&)));