How to get a text from a Qlineedit in Qt?

Asked

Viewed 1,952 times

4

For example, I have a LineEdit, when I press a button I want to pick up what I wrote on this LineEdit and store a QString.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

4

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

From there only create the command. Remembering that for those who use Qt Creator, you must use the ui because the commands must be accessed from it. If you do not use Qtcreator it is highly recommended to use it for learning.

Excerpt from Janelaprincipal.cpp

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

    qDebug() << mString ;
}

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

It could be done so, but you would have scope problems depending on the use:

void JanelaPrincipal::on_pushButton_clicked()
{
    QString mString;

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

    qDebug() << mString ;
}

3

  • That’s pretty much it.

1


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

Saída do programa

Following is a complete and commented code example:

Qlineedit.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = QLineEdit
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += 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); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.