How to get a text from a QLineEdit in Qt?

4

For example, I have a LineEdit , when I push a button I want to get what I wrote in this LineEdit and store a QString .

    
asked by anonymous 20.04.2015 / 12:30

3 answers

3

Generically so would this:

suaString = lineEdit->text(); //lineEdit é o objeto do editor
    
20.04.2015 / 14:28
3

With QtCreator: Create a button and a lineEdit. Then I do an action for it: go to slot - > clicked.

From there just create the command. Remember that, for those who use Qt Creator, you must use the ui because the commands should be accessed from it. If you do not use QtCreator it is highly recommended that you use it for learning.

Main Window excerpt.cpp

 void JanelaPrincipal::on_pushButton_clicked()
{
    mString = ui->lineEdit->text();

    qDebug() << mString ;
}

and create your variable mString in Janelaprincipal.h , private if it is used only in the class (usually it is). The above QDebug is only for capture testing.

It could be done this way, but you would have scope problems depending on usage:

void JanelaPrincipal::on_pushButton_clicked()
{
    QString mString;

    mString = ui->lineEdit->text();

    qDebug() << mString ;
}
    
20.04.2015 / 17:55
1

Basically what you need is to create a function in your program to be called every time a button click event is issued.

Hereisacompleteandcommentedcodeexample:

QLineEdit.pro

QT+=coreguigreaterThan(QT_MAJOR_VERSION,4):QT+=widgetsTARGET=QLineEditTEMPLATE=appSOURCES+=main.cpp\mainwindow.cppHEADERS+=mainwindow.h

main.cpp

#include"mainwindow.h"
#include <QApplication>

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

    //Toda GUI que utiliza QT precisa ter um objeto QApplication
    QApplication a(argc, argv);

    //Cria a janela principal
    MainWindow *window = new MainWindow;

    //Exibe a janela principal
    window->show();

    //Basicamente impede que o programa termine e sua janela seja destruida    
    return a.exec();
}

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <iostream>

class MainWindow : public QWidget {
    Q_OBJECT

private:
    QLineEdit *_QLineEdit;
    QPushButton *_QPushButton;

public:
    MainWindow();
    ~MainWindow();

public slots:
    void my_slot();
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow() {

    //Cria um layout
    QVBoxLayout *layout = new QVBoxLayout(this);

    //Defini o layout de MainWindow
    this->setLayout(layout);

    //Aloca memória dinamicamente para o editor de linha, definindo MainWindow como seu pai
    _QLineEdit = new QLineEdit(this);

    //Adiciona o editor de linha ao layout
    layout->addWidget(_QLineEdit);

    //Aloca memória dinamicamente para o botão, definindo MainWindow como seu pai
    _QPushButton = new QPushButton(this);

    //Adiciona o botão ao layout 
    layout->addWidget(_QPushButton);

    //Conecta o sinal de botão pressionado ao slot que criamos
    QObject::connect(_QPushButton, SIGNAL(clicked()), this, SLOT(my_slot())); 
}

MainWindow::~MainWindow() {
    /*
        Não há necessidade de adicionar instruções para liberar a memória alocada,
        pois isto será feito automaticamente uma vez que todos os objetos criados
        foram definidos como sendo filhos de MainWindow.
    */
}

void MainWindow::my_slot() {
    //Recupera o texto armazenado no QLineEdit
    QString myString(_QLineEdit->text()); 

    //Atribui o texto ao botão para testar
    _QPushButton->setText(myString); 
}
    
17.04.2016 / 02:40