如何将变量从一个表单传递到另一个表单
How can i pass the variable from one form to other form?
我创建了一个登录页面,当一个人输入他的用户名和密码时,现在我想要将该用户名传递到下一个打开表单中,以便制作个人仪表板。
代码在这里
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 中,然后从登录中发出带有用户名的插槽。
相关文章:
- GTK 最大化表单 C++
- win 表单应用程序字符串^ 到 int
- 如何将变量从一个表单传递到另一个表单
- 如何从另一个Qt Widget表单类调用MainWindow类
- 如何将变量从一个 qt 表单传递到另一个表单 qt 形式
- QT子类UI表单显示一个空白窗口
- 在C++中将文本从一个表单传递到另一个表单
- 嗨,我正在尝试编码一个允许用户在Visual C 中登录的按钮,程序正在运行,但表单对话框未显示
- 在Qt中从一个表单导航到另一个表单
- 在另一个类的表单上添加 QGraphicsView
- 将数据发送到另一个表单上的小部件
- 从Qt4上的另一个表单捕获形式的关闭事件
- 如何使用信号和插槽从qt表单中获取id,然后在链表中搜索它并使用另一个表单显示结果
- 打开另一个表单,然后等待该表单返回响应
- 是否有可能在qt中的一个类中切换两个UI表单?
- 为什么我们在Qt中处理表单时要为ui对象声明一个命名空间?
- 使用CreateDesktop/SwitchDesktop在新桌面中创建一个表单
- 从现有的表单打开一个新表单-这是可以接受的吗?
- 有什么方法可以通过Qt锁定除一个表单外的所有表单吗
- 如何将 Qt 表单类移动到另一个命名空间