Issue
I have a lot of ui
components in my .ui
file.
They have a similar name e.g analogRead0
, analogRead1
, analogRead2
and they have the same data type.
Is it possible for me to acces these fields inside the .ui
file by using only the name?
I was thinking of I can make an instance of an object by just entering the name of the object, and not the class. Is that possible in QT
?
For example, instead of writing
DoubleSpinBox *mySpinBox0 = ui->analogDifferentialInput0MaxDoubleSpinBox;
DoubleSpinBox *mySpinBox1 = ui->analogDifferentialInput1MaxDoubleSpinBox;
DoubleSpinBox *mySpinBox2 = ui->analogDifferentialInput2MaxDoubleSpinBox;
DoubleSpinBox *mySpinBox3 = ui->analogDifferentialInput3MaxDoubleSpinBox;
..
..
..
DoubleSpinBox *mySpinBoxN = ui->analogDifferentialInputNMaxDoubleSpinBox;
I can access all these DoubleSpinBoxes
by entering the name analogDifferentialInput
+ a number + MaxDoubleSpinBox
?
Solution
If you set the object name of the element in the Designer
, then you can directly access them via findChild (once you have set up the ui
…)
Here is an example, where I change the text for my 3 QPushButton
s
ui->setupUi(this);
for(int i = 1; i <= 3; ++i){
auto btn = findChild<QPushButton*>("button"+QString::number(i));
if(btn)
btn->setText("newName"+QString::number(i));
}
Answered By – Mat
Answer Checked By – Jay B. (BugsFixing Admin)