简体   繁体   中英

How can I change a class element via QLineEdit in QT?

I'm new to QT and trying to practice. It came to signals and slots. I'll get to the point. I have a User class: (User.h)

class User : public QObject
{
    Q_OBJECT
public:
    static int counter;

    explicit User(QObject *parent = nullptr);
    ~User();
    QString getName();
    QString getPassword();
    int getAge();
public slots:
    void setName(QString name);
    void setPassword(QString password);
    void setAge(int age);
protected:
    QString name_;
    QString password_;
    int *id_;
    int age_;

};
#include "User.h"


User::User(QObject* parent) :
   QObject(parent)
{
   counter++;
   id_ = new int(counter);
}

User::~User()
{
   delete id_;
}

QString User::getName()
{
   return name_;
}

QString User::getPassword()
{
   return password_;
}

int User::getAge()
{
   return age_;
}

void User::setName(QString name) {
   name_ = name;
}
void User::setPassword(QString password) {
   password_ = password;
}
void User::setAge(int age) {
   age_ = age;
}

int User::counter = 0;


Well, through QLineEdit (the entered line), you need to change the state of the elements in the class (name_, password_, age_. Well, I will skin the header of the QtWidgetsAppilication class:

#pragma once

#include <QtWidgets/QMainWindow>
#include <qmessagebox.h>

#include "ui_QtWidgetsApplication.h"
#include "User.h"

class QtWidgetsApplication : public QMainWindow
{
    Q_OBJECT

public:
    QtWidgetsApplication(QWidget *parent = Q_NULLPTR);
    void inputUser();
public slots:
    void inputUserName();
    void inputPassword();

private:
    Ui::QtWidgetsApplicationClass ui;
    User user;
};

(Know that * User user * was a bad idea) cpp:

#include "QtWidgetsApplication.h"

QtWidgetsApplication::QtWidgetsApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    connect(ui.input, &QPushButton::clicked, this, &QtWidgetsApplication::inputUser);
    connect(ui.login, &QLineEdit::text, this, &QtWidgetsApplication::inputUserName);
    connect(ui.password, &QLineEdit::text, this, &QtWidgetsApplication::inputPassword);
}

void QtWidgetsApplication::inputUserName()
{
    
    QObject::connect(ui.login, &QLineEdit::textChanged, &user, &User::setName);
}

void QtWidgetsApplication::inputPassword()
{
   
    QObject::connect(ui.password, &QLineEdit::textChanged, &user, &User::setPassword);
}

void QtWidgetsApplication::inputUser()
{
    if (user.getName() == "John" && user.getPassword() == "1234") {
        QMessageBox::information(this, "Programm", "You are our employee!");
    }

    else {
        QMessageBox::warning(this, "Program", "ERROR!");
    }

}

I read about signals and slots and I used the following syntax:

QObject::connect(ui.password, &QLineEdit::textChanged, &user, &User::setPassword);

However, the values ​​did not change for me, but it came to check, then the compiler saw garbage instead of the value and issued "ERROR" when it did not satisfy the condition. How can this problem be corrected?

connect(ui.login, &QLineEdit::text, this, &QtWidgetsApplication::inputUserName);
connect(ui.password, &QLineEdit::text, this, &QtWidgetsApplication::inputPassword);

First, text is not QLineEdit's signal.

QObject::connect(ui.login, &QLineEdit::textChanged, &user, &User::setName);
void setName(QString name);
void setPassword(QString password);

Second, the function setName's parameter type can not be QString, should be const QString&, because the textChanged's parameter type is const QString&.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM