Reputation: 337
I'm try to pass string value from mainwindow.cpp to userdetails.cpp. I had been used global variable. And When the program run it's show error message "Undefined references to globelusername". globelusername mean the global variable name. What is the error in code?
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "register.h"
#include "userdetails.h"
//#include <QtSql>
//#include <QSqlDatabase>
//#include <QMessageBox>
extern QString globelusername;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui ->loginusername->setPlaceholderText("Username");
ui ->loginpassword->setPlaceholderText("Password");
QString username = ui ->loginusername ->text();
QString globelusername = username;
return ;//
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
//open new registration in new window
hide();
regist = new Register(this);
regist ->show();
}
void MainWindow::on_pushButton_2_clicked()
{
//database connection
.....
QString username = ui ->loginusername ->text();
QString password = ui ->loginpassword ->text();
if(db.open()){
//query create
QSqlQuery query(QSqlDatabase::database("MyConnect"));
query.prepare(QString("SELECT * FROM user_reg_elec WHERE username = :username AND password = :password"));
query.bindValue(":username", username);
query.bindValue(":password", password);
QString globelusername = username; //globlevariable
}
userdetails.cpp
#include "userdetails.h"
#include "ui_userdetails.h"
#include <QSqlError>
#include "mainwindow.h"
Userdetails::Userdetails(QWidget *parent) :
QDialog(parent),
ui(new Ui::Userdetails)
{
ui->setupUi(this);
}
Userdetails::~Userdetails()
{
}
void Userdetails::on_pushButton_4_clicked()
{
{
// database connection
........
QString abc = globelusername;
Upvotes: 1
Views: 545
Reputation: 48258
you are shooting your own foot,
look carefully that MainWindows.h
is including a Userdetails.h
and user detail is including a MainWindows.h, that is called in the software dev. cyclic dependencies and is very very bad!
instead, define a Qstring as a member object/variable in the main Window AND in the Userdetails, define after that a setter in the UserDetails class, then in the mainWindows you can pass that as parameter:
in uderDetail
Userdetails::setName(const QString& name)
{
this->name=name;
}
and in the MainWindows
MainWindow::foo()
{
this->myUserDetails->setName(this->name);
}
and later do
this->myUserDetails->show(); //exec() or similar
Upvotes: 1
Reputation: 104464
You declared a global variable at the top of your header file
extern QString globelusername;
But you never define it.
You've got local definitions within functions, but those variables aren't the global one you probably think you are assigning to. They are just temporaries variables that go away when the enclosing scope of function returns:
QString globelusername = username; //globlevariable
To fix, define this at the top of mainwindow.cpp
:
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString globelusername; // add this line
MainWindow::MainWindow(QWidget *parent)
Then in all those places where you define globelusername within a function change it to just reference the variable at top (i.e. remove the QString type delclaration so the compiler knows it to be an assignment and not a new variable)
globelusername = username;
Upvotes: 1
Reputation: 2007
This line
extern QString globelusername;
just declares a global variable but does not define it.
You have to define it in one of your .cpp file (e.g. in mainwindow.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
QString globelusername;
// ...
Upvotes: 1