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. How to read struct members from another class
Forum Updated to NodeBB v4.3 + New Features

How to read struct members from another class

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 1.1k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    rezaMSLM
    wrote on last edited by
    #1

    I have a struct in my mainwindow class:

    #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
    
        #include "dialog.h"
        #include <QMainWindow>
    
        namespace Ui {
          class MainWindow;
        }
    
        class MainWindow : public QMainWindow
        {
          Q_OBJECT
      
          public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
        
            struct properties{
              int ID = -1;
            };
    
            properties ret_func();
          private slots:
            void on_btn1_clicked();
    
          private:
            Ui::MainWindow *ui;
            properties _properties;
            Dialog *_dialog;
        };
    
        #endif // MAINWINDOW_H
    

    I set value to it's memeber in mainwindow.cpp by clicking on btn1:

      MainWindow::properties MainWindow::ret_func()
        {
          return _properties;
        }
    
        void MainWindow::on_btn1_clicked()
        {
          _properties.ID = ui->lineEdit->text().toInt();
          qDebug()<<_properties.ID;
          _dialog->exec();
        }
    

    by clicking on btn1 another window is opened and I want to read the struct member value by clicking on btn2. This is my code:

     void Dialog::on_btn2_clicked()
        {
            qDebug()<<MainWindow::ret_func().ID;//->this line has error
        }
    

    Error:

        error: cannot call member function 'MainWindow::properties 
        MainWindow::ret_func()' without object
         qDebug()<<MainWindow::ret_func().ID;
                                        ^
    

    I have read some threads having similar problems but couldn't understand solution
    please help me

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      you need a pointer to the existing MainWindow and
      say
      qDebug()<< winPtr->ret_func().ID;

      However, since Dialog also needs _properties, why not give it to Dialog directly
      by extending the constructor to take such parameter.
      Dialog mydia(this, _properties );
      mydia.exec();

      R 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        you need a pointer to the existing MainWindow and
        say
        qDebug()<< winPtr->ret_func().ID;

        However, since Dialog also needs _properties, why not give it to Dialog directly
        by extending the constructor to take such parameter.
        Dialog mydia(this, _properties );
        mydia.exec();

        R Offline
        R Offline
        rezaMSLM
        wrote on last edited by
        #3

        @mrjj
        you mean this?

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            _dialog = new Dialog(this, _properties);
        }
        

        Error:

        error: no matching function for call to 'Dialog::Dialog(MainWindow*, MainWindow::properties&)'
             _dialog = new Dialog(this, _properties);
                                                   ^
        
        mrjjM 1 Reply Last reply
        0
        • R rezaMSLM

          @mrjj
          you mean this?

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
              _dialog = new Dialog(this, _properties);
          }
          

          Error:

          error: no matching function for call to 'Dialog::Dialog(MainWindow*, MainWindow::properties&)'
               _dialog = new Dialog(this, _properties);
                                                     ^
          
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rezaMSLM
          yes but you have to change the Dialogs constructor so it can take one.

          R 1 Reply Last reply
          1
          • mrjjM mrjj

            @rezaMSLM
            yes but you have to change the Dialogs constructor so it can take one.

            R Offline
            R Offline
            rezaMSLM
            wrote on last edited by
            #5

            @mrjj
            I don't get your meaning

            mrjjM 1 Reply Last reply
            0
            • R rezaMSLM

              @mrjj
              I don't get your meaning

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @rezaMSLM
              Show Dialogs constructor please :)

              R 1 Reply Last reply
              0
              • mrjjM mrjj

                @rezaMSLM
                Show Dialogs constructor please :)

                R Offline
                R Offline
                rezaMSLM
                wrote on last edited by rezaMSLM
                #7

                @mrjj

                public:
                    explicit Dialog(QWidget *parent = 0);
                

                :D

                mrjjM 1 Reply Last reply
                0
                • R rezaMSLM

                  @mrjj

                  public:
                      explicit Dialog(QWidget *parent = 0);
                  

                  :D

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @rezaMSLM
                  #include "MainWindow.h" // so it knows it.

                  explicit Dialog(QWidget *parent , MainWindow::properties & theProps );

                  also add the same in . cpp file

                  Note the &. if you omit that, it will get a copy.

                  R 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @rezaMSLM
                    #include "MainWindow.h" // so it knows it.

                    explicit Dialog(QWidget *parent , MainWindow::properties & theProps );

                    also add the same in . cpp file

                    Note the &. if you omit that, it will get a copy.

                    R Offline
                    R Offline
                    rezaMSLM
                    wrote on last edited by
                    #9

                    @mrjj
                    in my dialog.h:

                    public:
                        explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);
                        ~Dialog();
                    

                    in dialog.cpp

                    Dialog::Dialog(QWidget *parent, MainWindow::_properties &theProps) :
                        QDialog(parent),
                        ui(new Ui::Dialog)
                    {
                        ui->setupUi(this);
                    }
                    

                    But I receive Errors in these lines of mainwindow.h:

                    private:
                        Ui::MainWindow *ui;
                        properties _properties;// error: 'MainWindow::properties MainWindow::_properties' is private
                         properties _properties;
                                    ^
                        Dialog *_dialog;// error: 'Dialog' does not name a type Dialog *_dialog;
                    };
                    

                    and in dialog.h:

                    public:
                        explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);/* error: default argument missing for parameter 2 of 'Dialog::Dialog(QWidget*, int&)'
                         explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);
                                  ^
                    */
                        ~Dialog();
                    
                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi
                      Remove the =0
                      explicit Dialog(QWidget *parent = 0, MainWindow::_properties & theProps);
                      or swap the order of parameters if you want a default value.

                      for the
                      Dialog *_dialog;// error: 'Dialog' does not name a type Dialog *_dialog;

                      is due to us using Mainwindow.h in Dialog.h and made a circular includes. ( which is forbidden)

                      to fix it.
                      over MainWindow class.
                      type
                      class Dialog; // this is a forward
                      and move the #include "Dialog.h" to mainwindow. cpp file.

                      1 Reply Last reply
                      2

                      • Login

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