如何将变量从一个表单传递到另一个表单

How can i pass the variable from one form to other form?

本文关键字:表单 一个 另一个 变量      更新时间:2023-10-16

我创建了一个登录页面,当一个人输入他的用户名和密码时,现在我想要将该用户名传递到下一个打开表单中,以便制作个人仪表板。

代码在这里

admin_panel.cpp

#include "admin_panel.h"
#include "ui_admin_panel.h"
#include<QMessageBox>
#include "login.h"
admin_panel::admin_panel(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::admin_panel)
{
    ui->setupUi(this);

}
admin_panel::~admin_panel()
{
    delete ui;
}
void admin_panel::on_pushButton_clicked()
{
    Login conn;
    QMessageBox msg;
    QString Name,Surname,Role,Username, Password;
    Name = ui->txt_name->text();
    Surname = ui->txt_surname->text();
    Role = ui->txt_role->text();
    Username = ui->txt_username->text();
    Password = ui->txt_password->text();

    if(!conn.connOpen())
    {
        qDebug() << "No connection to db";
        return;
    }
     conn.connOpen();

    QSqlQuery qry;
    qry.prepare("INSERT INTO Users (Name,Surname,Role,Username,Password) VALUES(:Name,:Surname, :Role, :Username, :Password)");
    qry.bindValue(":Name", Name);
    qry.bindValue(":Surname", Surname);
    qry.bindValue(":Role", Role);
    qry.bindValue(":Username", Username);
    qry.bindValue(":Password", Password);
    if(qry.exec())
    {
        if(!qry.next())
        {
            msg.setText("Saved");
            msg.exec();
            conn.connClose();
        }
        else
        {
            QMessageBox::critical(this,tr("error::"),qry.lastError().text());
        }
    }
}

admin_panel.h 文件

#ifndef ADMIN_PANEL_H
#define ADMIN_PANEL_H
#include <QDialog>
namespace Ui {
class admin_panel;
}
class admin_panel : public QDialog
{
    Q_OBJECT

public:
    explicit admin_panel(QWidget *parent = 0);
    ~admin_panel();
private slots:
    void on_pushButton_clicked();
    void on_user_linkActivated(const QString &link);

private:
    Ui::admin_panel *ui;
};
#endif // ADMIN_PANEL_H

登录.h 文件

#ifndef LOGIN_H
#define LOGIN_H
#include <QMainWindow>
#include<QtSql/QtSql>
#include<QDebug>
#include <QDialog>
#define path "DB.sqlite"
namespace Ui {
class Login;
}
class Login : public QMainWindow
{
    Q_OBJECT
public:
    QSqlDatabase myDB;
    void connClose()
    {
        myDB.close();
        myDB.removeDatabase(QSqlDatabase::defaultConnection);
    }
    bool connOpen()
    {
        myDB = QSqlDatabase::addDatabase("QSQLITE");
        myDB.setDatabaseName(path);

            if(myDB.open())
            {
                qDebug()<<("[+]Connected to Database");
                return true;
            }
            else
            {
                qDebug()<<("[!] Database not found");
                return false;
            }
    }
public:
    explicit Login(QWidget *parent = 0);
    ~Login();

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
private:
    Ui::Login *ui;
};
#endif // LOGIN_H

登录.cpp文件

#include "login.h"
#include "ui_login.h"
#include "admin_panel.h"
#include<QMessageBox>
#define path "DB.sqlite"
Login::Login(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    QFileInfo checkfile(path);
   if(checkfile.isFile())
   {
       if(connOpen())
       {
           ui->label->setText("[+]Connected to Database");
       }
       else
       {
           ui->label->setText("[!] Database not found");
       }
  }
   ui->time->setText( QTime::currentTime().toString());
}

Login::~Login()
{
    delete ui;
}
void Login::on_pushButton_clicked()
{
    QString Username,Password;
    Username = ui->txt_user->text();
    Password = ui->txt_pass->text();
    if(!myDB.isOpen())
    {
        qDebug() << "No connection to db";
        return;
    }
   //connOpen();
    QSqlQuery qry;
    qry.prepare("SELECT Username, Password, Role FROM Users WHERE Username='" + Username + "' AND Password='" + Password + "'");
    if(qry.exec())
    {
        if(qry.next())
        {
            ui->label->setText("[+] Valid username and password");
            QString msg = "Username = " + qry.value(0).toString() + " n" +
                    "Role = " + qry.value(2).toString();
            QMessageBox::warning(this,"Login was successful",msg);
             connClose();
             this->hide();
             admin_panel admin;
             admin.setModal(true);
             admin.exec();          

        }
        else
        {
            ui->label->setText("[-]Wrong username or password");
        }
    }
}
void Login::on_pushButton_2_clicked()
{
    ui->txt_user->setText("");
    ui->txt_pass->setText("");
}

这是主要的.cpp

#include "login.h"
#include "admin_panel.h"
#include<QDialog>
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Login login;
    int q = 1;
    admin_panel admin(0);
    login.show();
    login.show();->admin();

    return a.exec();
}

现在从登录成功登录后.cpp文件。如何将用户名传递到以后可以在标签中使用的admin_panel.cpp

您可以使用 QT 的信号槽机制。 在登录类中创建一个 SIGNAL,在 admin_panel 中创建一个 SLOT,并将 QObject::connect(...( 放在 main.cpp 中,然后从登录中发出带有用户名的插槽。