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. Share data between two classes.

Share data between two classes.

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qt 5.7c++ windowsclass
6 Posts 3 Posters 2.9k 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

    How and what do i include in the header files so that i can get data from mainwindow.cpp into employee.cpp and vice versa.
    I want both the classes to share each others data , even the .ui file.
    It is working fine when i include one class in another , but it does not work when i include both in each other.I searched and i got that i have to do a forward declaration.
    But i don't understand how . Its really frustrating . Please help.

    mainwindow.h

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

    employeeinfo.h

    #ifndef EMPLOYEEINFO_H
    #define EMPLOYEEINFO_H
    #include <QMainWindow>
    
    
    namespace Ui {
    class employeeinfo;
    }
    
    class employeeinfo : public QMainWindow
    {
        Q_OBJECT
    
    public:
    
        explicit employeeinfo(QWidget *parent = 0);
        ~employeeinfo();
    
    private slots:
    
    private:
        Ui::employeeinfo *ui;
    };
    
    #endif // EMPLOYEEINFO_H
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exactly do you want to share ? For what purpose ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • ronyNSR Offline
        ronyNSR Offline
        ronyNS
        wrote on last edited by
        #3

        I want to share username variable from main window.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mjsurette
          wrote on last edited by
          #4

          @ronyNS said:

          want to share username variable from main window.

          You have several options besides coupling two classes, which is generally considered a bad idea.

          You can use setters and getters, pass the value in the constructor, or signals and slots. Signals and slots ties in quite well with event driven programming.

          Mike

          1 Reply Last reply
          2
          • ronyNSR Offline
            ronyNSR Offline
            ronyNS
            wrote on last edited by
            #5

            I just gave you 1 example about the username.
            There are many things that i want in employeeinfo.h , i even want the text field present in the mainwindow.ui.
            Is there a way where i can include mainwindow.h in employeeinfo.h and get the data i need from mainwindow.
            Thank you.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Just adding a header file here won't help much and you are doing it the wrong way around.

              Why should your EmployeeInfo class know anything from MainWindow ? That's only going to create a tight coupling and the time you'll want to change MainWindow for something else, you'll also have to change EmployeeInfo.

              If you are going to show EmployeeInfo from MainWindow, add setters to EmployeeInfo that you'll use to update its content before showing it like already suggested by @mjsurette or use properties/signals and slots.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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