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 different variables?
Qt 6.11 is out! See what's new in the release blog

Access to different variables?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.3k Views 2 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.
  • A Offline
    A Offline
    AlexKrammer
    wrote on last edited by
    #1

    Is there a possibility to access in one funktion to different variables.

    MainWindow HeaderFile:

    private:
    bool Test1 = false;
    bool Test2 = false;
    bool Test3 = false;
    .
    .
    .
    

    i just tried different way but it don´t work.

    MainWindow SourceFile:

    Try 1:
    void MainWindow::on_comboBox_currentIndexChanged(int x)
    {
            Test(x) = true;
    }
    Try 2:
    void MainWindow::on_comboBox_currentIndexChanged(int x)
    {
            "Test" + x = true;
    }
    Try 3:
    void MainWindow::on_comboBox_currentIndexChanged(int x)
    {
            Test + x = true;
    }
    

    cause in that case its working:

     for(int i = 1 ; i <= Initialisationlist.count() ; i++)
     {
            QLabel * label   = this->findChild<QLabel   *>("label_"+QString::number(i));
            QComboBox * CBox = this->findChild<QComboBox*>("CBoxSensor_"+QString::number(i));
    
            if(label && (Initialisationlist.at(i-1) != "w1_bus_master1"))
            {
                CBox->clear();
                CBox->addItem("N.C.");
                label->setText(Initialisationlist.at(i-1));
                for (int x=1 ; x <= Initialisationlist.count(); x++)
                {
                    CBox->addItem(QString::number(x));
                }
                CBox->setCurrentIndex(0);
                label->setVisible(true);
                CBox->setVisible(true);
            }
     }
    
    Pl45m4P 1 Reply Last reply
    0
    • A AlexKrammer

      @SGaist @Pl45m4

      I understand.
      But whats the right way to use a QVector in that case?

      and so that I can access the variable again later.

      i that way like @ollarch said the right way or do you think a other way would be better:

      QVector m_qTestVector;
      for (int i=0; i<3; i++)
        m_qTestVector.append(false);
      
      ...
      void MainWindow::on_comboBox_currentIndexChanged(int i)
      {
              m_qTestVector[i] = true;
      }
      

      and later i can call:?

       if (m_qTestVector[1] == 1)
      {
           ....
      }
      
      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #6

      @AlexKrammer

      Your vector needs to be a member of MainWindow in order to access it from every function.
      (m_ already suggests it)

      // Header
      QVector<bool> m_tests;
      
      // code
      for(int i = 0; i < 3; i++)
              m_tests.push_back(false); // or append
      
      
      void MainWindow::on_comboBox_currentIndexChanged(int i)
      {
              m_tests[i] = true;
      }
      
      

      @AlexKrammer said in Access to different variables?:

      if (m_qTestVector[1] == 1)

      Yes


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

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • O Offline
        O Offline
        ollarch
        wrote on last edited by ollarch
        #2

        Hi,

        You could use QList or QVector:

        QVector m_qTestVector;
        for (int i=0; i<3; i++)
          m_qTestVector.append(false);
        
        ...
        void MainWindow::on_comboBox_currentIndexChanged(int i)
        {
                m_qTestVector[i] = true;
        }
        
        1 Reply Last reply
        1
        • A AlexKrammer

          Is there a possibility to access in one funktion to different variables.

          MainWindow HeaderFile:

          private:
          bool Test1 = false;
          bool Test2 = false;
          bool Test3 = false;
          .
          .
          .
          

          i just tried different way but it don´t work.

          MainWindow SourceFile:

          Try 1:
          void MainWindow::on_comboBox_currentIndexChanged(int x)
          {
                  Test(x) = true;
          }
          Try 2:
          void MainWindow::on_comboBox_currentIndexChanged(int x)
          {
                  "Test" + x = true;
          }
          Try 3:
          void MainWindow::on_comboBox_currentIndexChanged(int x)
          {
                  Test + x = true;
          }
          

          cause in that case its working:

           for(int i = 1 ; i <= Initialisationlist.count() ; i++)
           {
                  QLabel * label   = this->findChild<QLabel   *>("label_"+QString::number(i));
                  QComboBox * CBox = this->findChild<QComboBox*>("CBoxSensor_"+QString::number(i));
          
                  if(label && (Initialisationlist.at(i-1) != "w1_bus_master1"))
                  {
                      CBox->clear();
                      CBox->addItem("N.C.");
                      label->setText(Initialisationlist.at(i-1));
                      for (int x=1 ; x <= Initialisationlist.count(); x++)
                      {
                          CBox->addItem(QString::number(x));
                      }
                      CBox->setCurrentIndex(0);
                      label->setVisible(true);
                      CBox->setVisible(true);
                  }
           }
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #3

          @AlexKrammer

          This is very basic C++ / OOP knowledge and not related to Qt ;-)

          Your variable name can not be a variable itself. So you can't access Test1 with

          int x = 1;
          Test + x = true; // Test + x does NOT equal Test1 variable
          

          Put all your Test- bools in one array (QVector for example) and use x as index to access them

          @AlexKrammer said in Access to different variables?:

          cause in that case its working:
          label_"+QString::number(i)

          This works, because it's still a string and you can append anything you want


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

          ~E. W. Dijkstra

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

            Hi,

            Adding to @Pl45m4:
            @AlexKrammer said in Access to different variables?:

            QLabel * label = this->findChild<QLabel *>("label_"+QString::number(i));

            You are not accessing any local variable here. You are looking down the tree of objects that are child of your widget. This is a different concept.

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

            A 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Adding to @Pl45m4:
              @AlexKrammer said in Access to different variables?:

              QLabel * label = this->findChild<QLabel *>("label_"+QString::number(i));

              You are not accessing any local variable here. You are looking down the tree of objects that are child of your widget. This is a different concept.

              A Offline
              A Offline
              AlexKrammer
              wrote on last edited by
              #5

              @SGaist @Pl45m4

              I understand.
              But whats the right way to use a QVector in that case?

              and so that I can access the variable again later.

              i that way like @ollarch said the right way or do you think a other way would be better:

              QVector m_qTestVector;
              for (int i=0; i<3; i++)
                m_qTestVector.append(false);
              
              ...
              void MainWindow::on_comboBox_currentIndexChanged(int i)
              {
                      m_qTestVector[i] = true;
              }
              

              and later i can call:?

               if (m_qTestVector[1] == 1)
              {
                   ....
              }
              
              Pl45m4P 1 Reply Last reply
              0
              • A AlexKrammer

                @SGaist @Pl45m4

                I understand.
                But whats the right way to use a QVector in that case?

                and so that I can access the variable again later.

                i that way like @ollarch said the right way or do you think a other way would be better:

                QVector m_qTestVector;
                for (int i=0; i<3; i++)
                  m_qTestVector.append(false);
                
                ...
                void MainWindow::on_comboBox_currentIndexChanged(int i)
                {
                        m_qTestVector[i] = true;
                }
                

                and later i can call:?

                 if (m_qTestVector[1] == 1)
                {
                     ....
                }
                
                Pl45m4P Offline
                Pl45m4P Offline
                Pl45m4
                wrote on last edited by Pl45m4
                #6

                @AlexKrammer

                Your vector needs to be a member of MainWindow in order to access it from every function.
                (m_ already suggests it)

                // Header
                QVector<bool> m_tests;
                
                // code
                for(int i = 0; i < 3; i++)
                        m_tests.push_back(false); // or append
                
                
                void MainWindow::on_comboBox_currentIndexChanged(int i)
                {
                        m_tests[i] = true;
                }
                
                

                @AlexKrammer said in Access to different variables?:

                if (m_qTestVector[1] == 1)

                Yes


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

                ~E. W. Dijkstra

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

                  @AlexKrammer said in Access to different variables?:

                  if (m_qTestVector[1] == 1)
                  {
                  ....
                  }

                  Since m_TestVector is supposed to be a vector of Boolean, you should write your if accordingly. Otherwise you are going to make your code uselessly hard to reason about.

                  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
                  2

                  • Login

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