需要帮助在QT中将QString值从一个屏幕传递到另一个屏幕吗 P.>我对QT是全新的,并且只对C++有一些基本的理解,但是我很难把用户名qStand变量从MixWOWO.CPP传递到CuutoLogin .CPP。我试着通过构造函数传递它,但这似乎不起作用,我正在努力理解插槽是如何工作的
main window.cpp需要帮助在QT中将QString值从一个屏幕传递到另一个屏幕吗 P.>我对QT是全新的,并且只对C++有一些基本的理解,但是我很难把用户名qStand变量从MixWOWO.CPP传递到CuutoLogin .CPP。我试着通过构造函数传递它,但这似乎不起作用,我正在努力理解插槽是如何工作的,qt,signals-slots,Qt,Signals Slots,main window.cpp MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); if (!connOpen()) ui->Status->setText("Failed to open the database"); else ui->
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!connOpen())
ui->Status->setText("Failed to open the database");
else
ui->Status->setText("Database Conneceted...");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonLogin()
{
QString username = ui->lineUser->text();
QString password = ui->linePassword->text();
if (!connOpen())
{
qDebug() << "Failed To Open the Database";
}
connOpen();
QSqlQuery qry;
qry.prepare("select * from owner where userid='"+username +"' and password='"+password +"' and rank='3'");
if (qry.exec())
{
int count = 0;
while (qry.next())
{
count++;
}
if(count ==1)
{
QMessageBox::information(this, "Login", "Username and password is correct");
hide();
connClose();
customerLogin *userLogin = new customerLogin();
userLogin->show();
}
else if (count != 1)
{
if(qry.exec("select * from owner where userid='"+username +"' and password='"+password +"' and rank='1'"))
{
int count = 0;
while (qry.next())
{
count++;
}
if(count ==1)
{
QMessageBox::information(this, "Login", "Username and password is correct");
hide();
connClose();
secAdminLogin *adminLogin = new secAdminLogin;
adminLogin -> show();
}
else
{
QMessageBox::warning(this,"Login", "Username and password is not correct!");
}
}
}
}
}
customerLogin::customerLogin(QWidget *parent) :
QDialog(parent),
ui(new Ui::customerLogin)
{
ui->setupUi(this);
}
customerLogin::~customerLogin()
{
delete ui;
}
void customerLogin::setUsername(QString username)
{
m_username = username;
}
创建customerLogin后,可以添加一个函数从MainWindow.cpp传递用户名,如下所示
customerLogin *userLogin = new customerLogin();
userLogin->setUsername(username);
userLogin->show();
您需要将此函数添加到customerLogin的.h和.cpp中
customerLogin.h
private:
QString m_username;
public:
void setUsername(QString username);
customerLogin.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
if (!connOpen())
ui->Status->setText("Failed to open the database");
else
ui->Status->setText("Database Conneceted...");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::buttonLogin()
{
QString username = ui->lineUser->text();
QString password = ui->linePassword->text();
if (!connOpen())
{
qDebug() << "Failed To Open the Database";
}
connOpen();
QSqlQuery qry;
qry.prepare("select * from owner where userid='"+username +"' and password='"+password +"' and rank='3'");
if (qry.exec())
{
int count = 0;
while (qry.next())
{
count++;
}
if(count ==1)
{
QMessageBox::information(this, "Login", "Username and password is correct");
hide();
connClose();
customerLogin *userLogin = new customerLogin();
userLogin->show();
}
else if (count != 1)
{
if(qry.exec("select * from owner where userid='"+username +"' and password='"+password +"' and rank='1'"))
{
int count = 0;
while (qry.next())
{
count++;
}
if(count ==1)
{
QMessageBox::information(this, "Login", "Username and password is correct");
hide();
connClose();
secAdminLogin *adminLogin = new secAdminLogin;
adminLogin -> show();
}
else
{
QMessageBox::warning(this,"Login", "Username and password is not correct!");
}
}
}
}
}
customerLogin::customerLogin(QWidget *parent) :
QDialog(parent),
ui(new Ui::customerLogin)
{
ui->setupUi(this);
}
customerLogin::~customerLogin()
{
delete ui;
}
void customerLogin::setUsername(QString username)
{
m_username = username;
}
另外,您正在使用调用connOpen()两次
if (!connOpen())
{
qDebug() << "Failed To Open the Database";
}
connOpen();
if(!connOpen())
{
qDebug()除了bio建议的方法外,您还可以在MainWindow.cpp中声明一个全局变量,并在customerLogin.cpp中将其声明为“extern”,以使其可访问
用于通过信号/插槽进行“用户名”通信
- 在MainWindow.h中声明一个带有一个QString参数的信号
- 在customerLogin.h中声明具有一个QString参数的插槽
- 创建customerLogin对象后,连接信号和插槽
- 在需要时(在本例中,在userLogin->show()之前)以“username”变量作为参数发出信号