1

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;
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Geo Tech
  • 307
  • 3
  • 11
  • 4
    You should not use a global variable for this in the first place. – Eric Jun 16 '20 at 09:51
  • more over, you are using qt, why don't you avoid the whole global, and try a signal/slot on the event you need? but indeed, you should still define your global, after you declare it :) – crsn Jun 16 '20 at 09:57
  • 1
    Does this answer your question? [How do I use extern to share variables between source files?](https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files) – Nicolas Dusart Jun 16 '20 at 10:02
  • 2
    no reason to do that like this... you are creating a spaghetti code with cyclic dependencies... – ΦXocę 웃 Пepeúpa ツ Jun 16 '20 at 10:19
  • Got it. Thank you – Geo Tech Jun 16 '20 at 10:22
  • I have one more question. When i try this "qDebug( globelusername.toLatin1() ) ", in the userdetails.cpp file get null value. – Geo Tech Jun 16 '20 at 12:08

3 Answers3

1

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;

// ...
Nicolas Dusart
  • 1,867
  • 18
  • 26
1

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;
selbie
  • 100,020
  • 15
  • 103
  • 173
  • I have one more question. When i try this "qDebug( globelusername.toLatin1() ) ", in the userdetails.cpp file get null value. – Geo Tech Jun 16 '20 at 11:22
1

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
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97