Cout in a loop for QLabel Widgets
-
Hi All,
I want to do something like the following:
int n=10; QMap <int, QLabel*>Names; for (int ii=0;ii<n;ii++) { Names[ii] = new QLabel("Name" ,ii); }
Output : Name1
Name2I know that
Names[ii] = new QLabel("Name" ,ii) ;
is a wrong syntax , I want to get the result like
cout << "Name" <<ii ;
How can it be achieved in Qt?
-
Names[ii] = new QLabel(QString("Name %1").arg(ii));
-
@Swati777999 said in Cout in a loop for QLabel Widgets:
QLabel Names[ii] = new QLabel("Name" ,ii);
Names[ii] = new QLabel("Name" ,ii);
-
@jsulm said in Cout in a loop for QLabel Widgets:
@Swati777999 said in Cout in a loop for QLabel Widgets:
QLabel Names[ii] = new QLabel("Name" ,ii);
Names[ii] = new QLabel("Name" ,ii);
This syntax gives me error.
-
@Swati777999 said in Cout in a loop for QLabel Widgets:
This syntax gives me error.
Of course. Please learn to read documentation (https://doc.qt.io/qt-5/qlabel.html)!
There is no such constructor in QLabel.
What do you actually want to do?
Do you want to show the number (ii) in the label? -
Names[ii] = new QLabel(QString("Name %1").arg(ii));
-
@jsulm said in Cout in a loop for QLabel Widgets:
@Swati777999 said in Cout in a loop for QLabel Widgets:
This syntax gives me error.
Of course. Please learn to read documentation (https://doc.qt.io/qt-5/qlabel.html)!
Yes, that's the reason why I've specifically mentioned it in my post. There's no such declaration in label for the output of
Name ii
. Is there any other way to achieve it? -
@Swati777999 said in Cout in a loop for QLabel Widgets:
Is there any other way to achieve it?
Please read my post above
-
@jsulm said in Cout in a loop for QLabel Widgets:
Names[ii] = new QLabel(QString("Name %1").arg(ii));
Works perfectly! Thanks.