Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using one variable in another class

Using one variable in another class

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++c++ qtc++ windows
4 Posts 2 Posters 5.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ronyNSR Offline
    ronyNSR Offline
    ronyNS
    wrote on last edited by
    #1

    I want to use QString username from mainwindow in the employee.cpp file and display it on a label. However when i declare it in the employeeinfo.cpp file i get this error:
    Invalid use of non static member.
    How can i access username variable in employeeinfo.cpp from mainwindow.h

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include<QLabel>
    #include <QMainWindow>
    #include<QPushButton>
    #include<QSqlDatabase>
    #include "employeeinfo.h"
    
    namespace Ui {
    class MainWindow;
    }
    
    
    class employeeinfo;
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        QString username,password; <<< this variable
    
    private slots:
        
    
    private:
        Ui::MainWindow *ui;
        
    };
    
    
    #endif // MAINWINDOW_H
    
    

    employeeinfo.h

    #ifndef EMPLOYEEINFO_H
    #define EMPLOYEEINFO_H
    #include <QMainWindow>
    #include "mainwindow.h"
    
    namespace Ui {
    class employeeinfo;
    }
    
    class MainWindow;
    
    class employeeinfo : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit employeeinfo(QWidget *parent = 0);
        ~employeeinfo();
    
    private slots:
        
    private:
        Ui::employeeinfo *ui;
    };
    
    #endif // EMPLOYEEINFO_H
    

    employeeinfo.cpp

    #include "employeeinfo.h"
    #include "ui_employeeinfo.h"
    
    employeeinfo::employeeinfo(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::employeeinfo)
    {
    ui->setupUi(this);
    
    QString a = MainWindow::username; <<< i get the error here
    }
    
    
    employeeinfo::~employeeinfo()
    {
        delete ui;
    }
    
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      good design practice require that employeeinfo should not even know MainWindow exists.

      Pass the username as an argument to the employeeinfo constructor.

      explicit employeeinfo(const QString& username, QWidget *parent = 0);
      
      employeeinfo::employeeinfo(const QString& username, QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::employeeinfo)
      {
      ui->setupUi(this);
      
      QString a = username; <<< i get the error here
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      ronyNSR 1 Reply Last reply
      0
      • VRoninV VRonin

        good design practice require that employeeinfo should not even know MainWindow exists.

        Pass the username as an argument to the employeeinfo constructor.

        explicit employeeinfo(const QString& username, QWidget *parent = 0);
        
        employeeinfo::employeeinfo(const QString& username, QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::employeeinfo)
        {
        ui->setupUi(this);
        
        QString a = username; <<< i get the error here
        }
        
        ronyNSR Offline
        ronyNSR Offline
        ronyNS
        wrote on last edited by
        #3

        @VRonin
        Hey Thanks

        But now i get this error : no matching function call to 'employeeinfo::employeeinfo()'

        when i declare an object to show employeeinfo window from mainwindow
        employeeinfo *emp = new employeeinfo();

        i declared this is mainwindow.cpp

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          that's because there is where you need to pass the username

          employeeinfo *emp = new employeeinfo(username);
          

          Given the doubts you are having my advice is to take a quick look to a general C++ tutorial like http://www.cplusplus.com/doc/tutorial/ before diving into Qt

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved