E
E
esy19932015-07-03 18:14:39
Qt
esy1993, 2015-07-03 18:14:39

How to write the value of QLineEdit to a variable?

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

authorization.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 specifies a slot for receiving 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 the slot is triggered, which should 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 when building this code, 2 errors occur: 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 form, the names of the QLineEdit objects are exactly the same as in the cpp code. How to solve this problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
esy1993, 2015-07-03
@esy1993

Solved the problem in this way
:

void on_autorisOK_clicked()

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

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

}

G
Glucke, 2015-07-03
@Glucke

Try this.
void on_autorisOK_clicked()
{
QString userName = ui->autorizUser->text();
QString userPass = ui->autorizPass->text();
}

V
Vladimir Martyanov, 2015-07-03
@vilgeforce

If you have on_autorisOK_clicked() - a slot (below in the text, by the way, a different name), then it must be in the class, and not just like that. Next, the GUI is accessed via ui.element_name->...

V
Vitaly, 2015-07-03
@vt4a2h

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.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question