How do I write the QLineEdit value to a variable?

  • 0
    The following files attempt to implement a user authorization dialog in the database:
    autorization.h
    #ifndef AUTORIZATION_H
    #define AUTORIZATION_H
    
    #include <QWidget>
    
    namespace Ui {
    class Autorization;
    }
    
    class Autorization : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit Autorization(QWidget *parent = 0);
        ~Autorization();
    
    private slots:
    
    void on_autorizOK_clicked();
    
    private:
        Ui::Autorization *ui;
    };
    
    #endif // AUTORIZATION_H

    autorization.cpp
    #include "autorization.h"
    #include "ui_autorization.h"
    #include "autorization.h"
    #include <QtSql/QSqlQuery>
    #include <QSqlDatabase>
    #include <QDebug>
    #include <QSqlError>
    #include "QString"
    
    Autorization::Autorization(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Autorization)
    {
        ui->setupUi(this);
    
    }
    
    Autorization::~Autorization()
    {
        delete ui;
    }
    
    void on_autorisOK_clicked()
    
    {
        QString userName = autorizUser->text();
        QString userPass = autorizPass->text();
    }


    The autorization.h file contains a slot for accepting a signal from the "OK" button of the screen form: void on_autorizOK_clicked () ;. The autorization.cpp file contains a function that is executed when a slot is triggered, which must create 2 QString objects - userName and userPass and assign them the values ​​of the autorizUser and autorizPass fields, respectively. After that, it is planned to use variables as username and password when calling db.setUserName and db.setPassword. But building this code produces 2 errors: autorizUser / autorizPass was not declared in this scope.
    Am I correctly translating the error into Russian - "Variables are not declared within the procedure"? On the screen, the names of the QLineEdit objects are exactly the same as in the cpp code. How to solve this problem?
    C++ Nathan Rich, Aug 4, 2019

  • 4 Answers
  • 0
    Solved the problem this way:

    It was
    void on_autorisOK_clicked()

    {
    QString userName = autorizUser->text();
    QString userPass = autorizPass->text();
    }


    Now
    void Autorization::on_autorizOK_clicked()
    {
    QString userName = ui->autorizUser->text();
    QString userPass = ui->autorizPass->text();

    }
    Anonymous

  • 0
    Try this.

    void on_autorisOK_clicked ()


    {

    QString userName = ui- & gt; autorizUser- & gt; text ();

    QString userPass = ui- & gt; autorizPass- & gt; text ();

    }
    Anonymous

  • 0
    If you have an on_autorisOK_clicked () slot (below in the text, by the way, a different name), then it should be in the class, and not just like that. Further, the gui is accessed via ui.element_name- & gt; ...
    Anonymous

  • 0
    void Autorization::on_autorisOK_clicked()
    {
    QString userName = ui->autorizUser->text();
    QString userPass = ui->autorizPass->text();
    }


    Tip: first understand at least the basics of OOP in C ++, and then start working with Qt.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!