我有一个登录窗口,用户可以在其中输入一些文本。
登录.cpp:
#include "login.h"
#include "ui_login.h"
#include "mainwindow.h"
login::login(QWidget *parent) :
QDialog(parent),
ui(new Ui::login)
{
ui->setupUi(this);
this->setFixedSize(320,212);
connect(ui->_login, SIGNAL(clicked()), this, SLOT(rLogin()));
connect(ui->_close, SIGNAL(clicked()), this, SLOT(rClose()));
}
login::~login()
{
delete ui;
}
void login::rLogin()
{
prefix = ui->_prefix->text();
site = ui->_site->text();
dbname = ui->_database->text();
user = ui->_username->text();
QString pass = ui->_password->text();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(site);
db.setDatabaseName(prefix + dbname);
db.setUserName(prefix + user);
db.setPassword(pass);
bool logged = db.open();
if (logged) {
rOpen();
} else {
int warning = QMessageBox::warning(this, tr("Hostel ERP Error"),
tr("Could not connect to database.\n"
"Please check your inputs."),
QMessageBox::Ok);
}
}
void login::rOpen()
{
mainwindow* openwindow = new mainwindow();
openwindow->show();
this->close();
}
void login::rClose()
{
this->close();
}
prefix、site、dbname 和 user 在 login.h login.h 中定义为 QString:
#ifndef LOGIN_H
#define LOGIN_H
#include <QDialog>
namespace Ui {
class login;
}
class login : public QDialog
{
Q_OBJECT
public:
explicit login(QWidget *parent = 0);
void rOpen();
QString prefix;
QString site;
QString dbname;
QString user;
~login();
我正在尝试从 mainwindow.cpp 访问前缀、站点、数据库名和用户
主窗口.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSql>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QPushButton>
#include <QAction>
#include <QMainWindow>
#include <QSqlRecord>
#include <login.h>
namespace Ui {
class mainwindow;
}
class mainwindow : public QMainWindow
{
Q_OBJECT
public:
explicit mainwindow(QWidget *parent = 0);
void rPopulate();
QPushButton *button;
login * log;
~mainwindow();
主窗口.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "login.h"
#include "newbooking.h"
#include <qaction.h>
//somecode
login *log = new login;
QString table_schema = log->prefix + log->dbname;
但是 log->prefix 和 log->dbname 都是空的。ui->_prefix->text()中有默认文本;和 ui->_database->text(); 所以他们不应该是空的。