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. Access to other class variables

Access to other class variables

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 497 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.
  • M Offline
    M Offline
    Mark58
    wrote on last edited by
    #1

    Hi
    In my mainwindow I have a qtabwidget. Each tab has its own .ccp/.h/.ui - file and should show the result of a different network-request with different parameters and a different request method. The data (parameters, request-method) is in a QString in the .cpp file of the tabwidget.
    So when a tabwidget emits the signal "currentChanged", it calls a network-request-function and the class:

    my mainwindow.h contains:

    #include "tab_balance.h"
    #include "tab_charges.h"
    [...]
    public slots:
            void network_request(QWidget *tab_target);
    private:
        Ui::MainWindow *ui;
        
        tab_balance _gotobalances;
        tab_charges _gotocharges;
    

    my mainwindow.cpp contains:

    connect(ui->tabWidget, &QTabWidget::currentChanged, [&]() {
            if (ui->tabWidget->currentWidget()->objectName() == _gotobalances.objectName()) {
                network_request(&_gotobalances);
            }
            else if (ui->tabWidget->currentWidget()->objectName() == _gotocharges.objectName()){
                  network_request(&_gotocharges);
            }
    });
    [...]
    void MainWindow::network_request(QWidget *tab_target)  
    {
    [...]
    connect(manager, SIGNAL(finished(QNetworkReply*)), tab_target, SLOT(make_request_answer(QNetworkReply*)));
    }
    

    my tab_charges.h and tab_balance.h contain:

    public:
            explicit tab_charges(QWidget *parent = nullptr);
            ~tab_charges();
        
            QString tab_URL;
            QMap<QString, QString> parameters;
            QString endpoint;
       
        private slots:
            void make_request_answer(QNetworkReply *reply);
    

    My tab_charges.cpp and tab_balance.cpp contain:

    void tab_balance::make_request()
    {
        //endpoint
        endpoint = "get";  // or endpoint="post"
    
        //request-URL
        tab_URL = "https://api.example.com/[...]";
    
        //parameters
        parameters.insert("limit","4");  //or other/additional parameters
    }
    

    The values of the three QStrings in "void tab_balance::make_request()" are different in the .cpp-files (other method, other parameters).

    void tab_balance::make_request_answer(QNetworkReply *reply)
    {
        if(reply->error())
            {
                ui->textBrowser->setText(reply->errorString());
            }
            else
            {
                ui->textBrowser->setText(reply->readAll());
        }
    }
    

    The connect works as it should - it connects to tab_charges.cpp/tab_balance.cpp to the private slot "make_request_answer..." depending on the tabwidget that changed.

    However, I can not access the values of the three QStrings (in tab_charges/tab_balace) from "void MainWindow::network_request(QWidget *tab_target)".
    I need to access these values, because e.g. the request-method is different and I have to define the method before the network-request starts.

    This is a debug-result when qtabwidget "tab_charges" is active:

    qDebug() << "classnames" << tab_target->metaObject()->className() << "---" << _gotocharges.metaObject()->className();
    
    //result: "classnames tab_charges --- tab_charges"
    

    How can I access the three QStrings of the respective .cpp-file without if-clauses like (if (tab_target->metaObject()->className() == _gotocharges.metaObject()->className()) [...]). There should be a better way, because the "connect" works without all that.

    Thanks for your help

    JonBJ 1 Reply Last reply
    0
    • M Mark58

      Hi
      In my mainwindow I have a qtabwidget. Each tab has its own .ccp/.h/.ui - file and should show the result of a different network-request with different parameters and a different request method. The data (parameters, request-method) is in a QString in the .cpp file of the tabwidget.
      So when a tabwidget emits the signal "currentChanged", it calls a network-request-function and the class:

      my mainwindow.h contains:

      #include "tab_balance.h"
      #include "tab_charges.h"
      [...]
      public slots:
              void network_request(QWidget *tab_target);
      private:
          Ui::MainWindow *ui;
          
          tab_balance _gotobalances;
          tab_charges _gotocharges;
      

      my mainwindow.cpp contains:

      connect(ui->tabWidget, &QTabWidget::currentChanged, [&]() {
              if (ui->tabWidget->currentWidget()->objectName() == _gotobalances.objectName()) {
                  network_request(&_gotobalances);
              }
              else if (ui->tabWidget->currentWidget()->objectName() == _gotocharges.objectName()){
                    network_request(&_gotocharges);
              }
      });
      [...]
      void MainWindow::network_request(QWidget *tab_target)  
      {
      [...]
      connect(manager, SIGNAL(finished(QNetworkReply*)), tab_target, SLOT(make_request_answer(QNetworkReply*)));
      }
      

      my tab_charges.h and tab_balance.h contain:

      public:
              explicit tab_charges(QWidget *parent = nullptr);
              ~tab_charges();
          
              QString tab_URL;
              QMap<QString, QString> parameters;
              QString endpoint;
         
          private slots:
              void make_request_answer(QNetworkReply *reply);
      

      My tab_charges.cpp and tab_balance.cpp contain:

      void tab_balance::make_request()
      {
          //endpoint
          endpoint = "get";  // or endpoint="post"
      
          //request-URL
          tab_URL = "https://api.example.com/[...]";
      
          //parameters
          parameters.insert("limit","4");  //or other/additional parameters
      }
      

      The values of the three QStrings in "void tab_balance::make_request()" are different in the .cpp-files (other method, other parameters).

      void tab_balance::make_request_answer(QNetworkReply *reply)
      {
          if(reply->error())
              {
                  ui->textBrowser->setText(reply->errorString());
              }
              else
              {
                  ui->textBrowser->setText(reply->readAll());
          }
      }
      

      The connect works as it should - it connects to tab_charges.cpp/tab_balance.cpp to the private slot "make_request_answer..." depending on the tabwidget that changed.

      However, I can not access the values of the three QStrings (in tab_charges/tab_balace) from "void MainWindow::network_request(QWidget *tab_target)".
      I need to access these values, because e.g. the request-method is different and I have to define the method before the network-request starts.

      This is a debug-result when qtabwidget "tab_charges" is active:

      qDebug() << "classnames" << tab_target->metaObject()->className() << "---" << _gotocharges.metaObject()->className();
      
      //result: "classnames tab_charges --- tab_charges"
      

      How can I access the three QStrings of the respective .cpp-file without if-clauses like (if (tab_target->metaObject()->className() == _gotocharges.metaObject()->className()) [...]). There should be a better way, because the "connect" works without all that.

      Thanks for your help

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Mark58 said in Access to other class variables:

      How can I access the three QStrings of the respective .cpp-file without if-clauses

      There are many ways to skin a cat :) I believe what you are saying is: "I have multiple tab widgets. Each one has its own relevant data. The slot handling the signal needs to be back in the main window, but needs to know what that data is, given it knows which tab has been clicked."

      So what about subclassing the widgets placed on the tabs so that they share a common base class which offers a method that returns the necessary data? Like:

      MyDataWidget *w = qobject_cast<MyDataWidget *>ui->tabWidget->currentWidget();
      if (w != nullptr)
          relevantData = w->tabRelevantDataCommonMethod();
      
      M 1 Reply Last reply
      1
      • JonBJ JonB

        @Mark58 said in Access to other class variables:

        How can I access the three QStrings of the respective .cpp-file without if-clauses

        There are many ways to skin a cat :) I believe what you are saying is: "I have multiple tab widgets. Each one has its own relevant data. The slot handling the signal needs to be back in the main window, but needs to know what that data is, given it knows which tab has been clicked."

        So what about subclassing the widgets placed on the tabs so that they share a common base class which offers a method that returns the necessary data? Like:

        MyDataWidget *w = qobject_cast<MyDataWidget *>ui->tabWidget->currentWidget();
        if (w != nullptr)
            relevantData = w->tabRelevantDataCommonMethod();
        
        M Offline
        M Offline
        Mark58
        wrote on last edited by
        #3

        @JonB Thanks for your answer. However, I'm not sure if this is the best solution for me.

        In other words: I have the class name as a string

        QString classname = tab_target->metaObject()->className();
        

        And in the next step I want to get access to the value of a string of this class. So I need "something" that gives my string access to the class.

        Christian EhrlicherC JonBJ 2 Replies Last reply
        0
        • M Mark58

          @JonB Thanks for your answer. However, I'm not sure if this is the best solution for me.

          In other words: I have the class name as a string

          QString classname = tab_target->metaObject()->className();
          

          And in the next step I want to get access to the value of a string of this class. So I need "something" that gives my string access to the class.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mark58 Basic c++ - one class needs the pointer of another class if it wants access it's functions. So give the one class the pointer to the other and you're done.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          M 1 Reply Last reply
          1
          • M Mark58

            @JonB Thanks for your answer. However, I'm not sure if this is the best solution for me.

            In other words: I have the class name as a string

            QString classname = tab_target->metaObject()->className();
            

            And in the next step I want to get access to the value of a string of this class. So I need "something" that gives my string access to the class.

            JonBJ Online
            JonBJ Online
            JonB
            wrote on last edited by JonB
            #5

            @Mark58 said in Access to other class variables:

            In other words: I have the class name as a string
            And in the next step I want to get access to the value of a string of this class. So I need "something" that gives my string access to the class.

            You are coding in C++, not Python. That cannot be done in the C language, by design! And also you sound like you're confusing classes with instances. You can go down @Christian-Ehrlicher's approach, or mine, but string --> class/instance is a no-no :)

            1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

              @Mark58 Basic c++ - one class needs the pointer of another class if it wants access it's functions. So give the one class the pointer to the other and you're done.

              M Offline
              M Offline
              Mark58
              wrote on last edited by
              #6

              @Christian-Ehrlicher
              Thanks for your hint.
              However, I have no idea how to give the pointer to the other class.
              From mainwindow.cpp I do have access to the variables in tab_charges.cpp/tab_balance.cpp when I use

              _gotobalances->endpoint or _gotochages->endpoint
              

              Now I made the following changes:

              tab_balance *_gotobalances = new tab_balance;
              tab_charges *_gotocharges = new tab_charges;
              

              When I am in

              void MainWindow::network_request(QWidget *tab_target) 
              

              I only know the class name (either "tab_charges" or "tab_balance") which comes from "tab_target->objectName()". tab_target->endpoint does not exist.

              How do I have to set the pointer to access the appropriate "endpoint"? I appreciate your help.

              Pl45m4P 1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by Christian Ehrlicher
                #7

                I really really would suggest you a god good C++ book, sorry. This is basic c++ which is needed for Qt.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                4
                • M Mark58

                  @Christian-Ehrlicher
                  Thanks for your hint.
                  However, I have no idea how to give the pointer to the other class.
                  From mainwindow.cpp I do have access to the variables in tab_charges.cpp/tab_balance.cpp when I use

                  _gotobalances->endpoint or _gotochages->endpoint
                  

                  Now I made the following changes:

                  tab_balance *_gotobalances = new tab_balance;
                  tab_charges *_gotocharges = new tab_charges;
                  

                  When I am in

                  void MainWindow::network_request(QWidget *tab_target) 
                  

                  I only know the class name (either "tab_charges" or "tab_balance") which comes from "tab_target->objectName()". tab_target->endpoint does not exist.

                  How do I have to set the pointer to access the appropriate "endpoint"? I appreciate your help.

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @Mark58

                  Pass the instance with constructor.

                  A *a = new A();
                  B *b = new B(a);
                  

                  Something like that :)
                  Of course you have to alter the header files accordingly.

                  To access private vars from your class, you need public getters and setters. Even if you have the pointer to that class, it doesn't mean, that you can access everything.

                  Add public functions to your tab_targets that allow you to access your private data.

                  // Tab_balance / charges header
                  {
                  QString endpoint(){ return m_endpoint; }
                  // where "m_endpoint" = actual name of your private endpoint  string variable
                  }
                  

                  Then you can do this:
                  tab_balance->endpoint(); and you will receive the string that is stored in tab_balance.

                  Btw:
                  @JonB s first reply already provides the "cleanest" solution. All you have to do, is to create a base class for your tabTargets with has data access functions. Then it doesn't matter anymore which of your subclasses is currently active to get your data.

                  @Christian-Ehrlicher
                  I guess a good C++ book would be sufficient. He doesn't need to learn from a C++ god :D ;-)


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  Christian EhrlicherC 1 Reply Last reply
                  3
                  • Pl45m4P Pl45m4

                    @Mark58

                    Pass the instance with constructor.

                    A *a = new A();
                    B *b = new B(a);
                    

                    Something like that :)
                    Of course you have to alter the header files accordingly.

                    To access private vars from your class, you need public getters and setters. Even if you have the pointer to that class, it doesn't mean, that you can access everything.

                    Add public functions to your tab_targets that allow you to access your private data.

                    // Tab_balance / charges header
                    {
                    QString endpoint(){ return m_endpoint; }
                    // where "m_endpoint" = actual name of your private endpoint  string variable
                    }
                    

                    Then you can do this:
                    tab_balance->endpoint(); and you will receive the string that is stored in tab_balance.

                    Btw:
                    @JonB s first reply already provides the "cleanest" solution. All you have to do, is to create a base class for your tabTargets with has data access functions. Then it doesn't matter anymore which of your subclasses is currently active to get your data.

                    @Christian-Ehrlicher
                    I guess a good C++ book would be sufficient. He doesn't need to learn from a C++ god :D ;-)

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Pl45m4 Thx, fixed :)

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    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