我创建了一个登录页面,当一个人输入他的用户名和密码时,访问权限现在我想将该用户名传递给下一个开放表单,以便制作个人仪表板。
代码在这里
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
login.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
login.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("");
}
这是main.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();
}
从login.cpp文件成功登录后,。如何将用户名传递到admin_panel.cpp中,以后我可以在标签中使用
答案 0 :(得分:0)
您可以使用QT的信号槽机制。在登录类中创建SIGNAL,在 admin_panel 中创建SLOT,并在main.cpp中放置 QObject :: connect(...),然后发出slot使用登录中的userName。