-1

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

dtech
  • 47,916
  • 17
  • 112
  • 190
lightshadown
  • 165
  • 1
  • 1
  • 10

2 Answers2

3

Well, for starters, ClienteNo is a pointer to nothing. Even if it was a pointer to a QLineEdit, ClienteNo->lineClienteNo.text(); would not work because ClienteNo is a pointer to a QLineEdit which doesn't have a lineClienteNo member, but even if it had it, it would have no effect because you aren't doing anything with the returned value.

It looks like you are using a designer generated form for your widget, so in order to access it from the outside, you need to make your ui pointer in Ventas2.h public. Then you can:

QString text = VentanaPrincipal.ui->lineClienteNo->text();
// do something with text

Sounds like you need to familiarize yourself a little more with both the programming language and the framework before you try to use them in practice.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Sorry if the question sounded to noob, just learning c++ and tought Qt would be a good idea to use, will check the Ventas2.h class when i get out of work, thanks – lightshadown Dec 16 '16 at 19:00
0

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&)));
lightshadown
  • 165
  • 1
  • 1
  • 10