Save the text of a text box in a string

0

I am doing a program in QT in order to create a form and be able to save the information in a document. I have created my View for the form (where the user will enter the name), but now I do not know how to save what is written there in a String. Can you help me? Thanks !!

Clarification: my goal is to save the text that is written in the QLineEdit in a by pressing a pushButton in a string.

Clarification2: if I jump to the function saveTheText when I press the button but I can not save the message or display it.

Here the header of my View:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QFormLayout>
#include <QLabel>
#include <QGridLayout>
#include <QGroupBox>
#include <QDialog>
#include <QLineEdit>

class QGroupBox;
class QLabel;

class MainWindow : public QMainWindow
{
   Q_OBJECT
public:
    MainWindow(QWidget *parent=nullptr);
private slots:
    void saveTheText();
private:

    QGroupBox *myGroup;
    QPushButton *p_saveButton;
    QLineEdit *p_editName;
    QString *getName;

};
#endif // MAINWINDOW_H

Here the .cpp of my View:

#include "MainWindow.h"
#include "ModelOne.h"

#include <QtWidgets>
#include <QMainWindow>
#include <QTreeView>
#include <iostream>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow (parent)
{
    std::cout<<"HOLA"<<std::endl;

    QWidget *p_theCentralWidget = new QWidget(this);

    QGridLayout *p_myGrid = new QGridLayout(p_theCentralWidget);

    QLabel *p_labelName = new QLabel("Name", this);
    p_myGrid->addWidget(p_labelName,0,0);

    p_editName = new QLineEdit(this); //Por poner ponemos "this", pero puede ser "p_theCentralWidget"
    p_myGrid->addWidget(p_editName, 0, 1);

    p_saveButton=new QPushButton("Save name",this);
    p_myGrid->addWidget(p_saveButton,0,2);
    connect(p_saveButton,SIGNAL(clicked()),this,SLOT(saveTheText()));

    setCentralWidget(p_theCentralWidget);
}

void MainWindow::saveTheText()
{

    std::cout<<"HOLA222"<<std::endl;

    p_editName->displayText();

//    *getName=p_editName->text();
//    std::cout<<getName<<std::endl;

//    QString miString("ho");
//    miString=p_editName->text();


}

The main:

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow window;
    window.show();

    return a.exec();
}

And the window that I get when executing it:

    
asked by MAP 04.10.2018 в 14:56
source

1 answer

2

To retrieve the text of a QLineEdit you must use the text() method:

void MainWindow::saveTheText()
{
  QString text = p_editName->text();
  qDebug << text;
}
    
answered by 04.10.2018 / 15:49
source