PDA

View Full Version : How to create lineEdits as an array?



Wolletje
16th November 2016, 11:31
I am a newby at Qt
I have been searching extensively in the large amount of Qt-documentation and got lost.
In Qt Creator lineEdits are named as lineEdit, lineEdit_2 etc
Can anyone tell me if it is possible to create QLineEdit widgets with names as lineEdit[i] which can be called by a for-loop.
ui->lineEdit[i]-> setText(something[i]);
(Reference to) a short simple example would be very welcome.

Lesiok
16th November 2016, 11:37
Yes but not in Qt Creator. You must do this programmatically, more or less like this :

QList<QLineEdit*> qle_list;
for( int i=0; i < 100; i++ )
qle_list.append(new QLineEdit());

Wolletje
16th November 2016, 11:41
Thank you Lesiok, I am going to try.
But when not using Creator, in which file should it be placed?
I am up to now only familiar with Creator

Lesiok
16th November 2016, 13:24
In attachment simple example.

anda_skoa
16th November 2016, 15:55
Yes but not in Qt Creator.

You probably meant "not in Qt Designer"

Cheers,
_

Lesiok
16th November 2016, 17:14
You probably meant "not in Qt Designer"

Cheers,
_
Of course ;)

Wolletje
16th November 2016, 22:14
Thank you for your help, Lesiok.
I am going to try the codes from superdialog.zip

When I tried to implement the code you gave before into a widget.cpp indeed qle_list was constructed as a QList of type QLineEdit, but the elements were undefined and I could not find how to get the lineEdits on a window.
It is strange that I could nowhere find a straightforward solution. Until now I just use a large series of code lines like:
QLineEdit * text[50];
text[1] = ui->lineEdit_1
text[2] = ui->lineEdit_2
......
text[50] =ui->lineEdit_50

I can imagine that it can not be done easier with a nice system as Qt.
But maybe that is why I am still a newbe

rawfool
17th November 2016, 05:44
I guess you are trying to do this.


#include <QApplication>
#include <QDialog>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QList>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QDialog w;
QVBoxLayout * vLayout = new QVBoxLayout(&w);
QList<QLineEdit *> qle_list;

for(int i = 0; i < 10; ++i)
{
qle_list.append(new QLineEdit());
qle_list.at(i)->setObjectName(QString("lineEdit%1").arg(i)); // It's easier to recognize an object by name
vLayout->addWidget(qle_list.at(i));
}
w.setLayout(vLayout);
w.show();

return a.exec();
}

For example, you can get the object name by
for-loop: qle_list.at(i)->objectName();

Lesiok
17th November 2016, 06:47
Thank you for your help, Lesiok.
I am going to try the codes from superdialog.zip

When I tried to implement the code you gave before into a widget.cpp indeed qle_list was constructed as a QList of type QLineEdit, but the elements were undefined and I could not find how to get the lineEdits on a window.
It is strange that I could nowhere find a straightforward solution. Until now I just use a large series of code lines like:
QLineEdit * text[50];
text[1] = ui->lineEdit_1
text[2] = ui->lineEdit_2
......
text[50] =ui->lineEdit_50

I can imagine that it can not be done easier with a nice system as Qt.
But maybe that is why I am still a newbe
I do not think you understand my example. QLineEdit objects are created in the code and not in the UI file.

anda_skoa
17th November 2016, 10:11
If you want to create the buttons in Qt Designer, you can still retrieve them into a list using QObject::findChildren().

Cheers,
_

Alex22
20th November 2016, 07:13
I am a newby at Qt
I have been searching extensively in the large amount of Qt-documentation and got lost.
In Qt Creator lineEdits are named as lineEdit, lineEdit_2 etc
Can anyone tell me if it is possible to create QLineEdit widgets with names as lineEdit[i] which can be called by a for-loop.
ui->lineEdit[i]-> setText(something[i]);
(Reference to) a short simple example would be very welcome.

You can use this code: (Qt example in QSignalMapper):


QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}