Access to different variables?
-
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); } }
-
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
-
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
withint x = 1; Test + x = true; // Test + x does NOT equal Test1 variable
Put all your
Test
- bools in one array (QVector
for example) and usex
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
-
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.
-
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) { .... }
-
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
-
@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.