How to create lineEdits as an array?
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.
Re: How to create lineEdits as an array?
Yes but not in Qt Creator. You must do this programmatically, more or less like this :
Code:
QList<QLineEdit*> qle_list;
for( int i=0; i < 100; i++ )
Re: How to create lineEdits as an array?
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
1 Attachment(s)
Re: How to create lineEdits as an array?
In attachment simple example.
Re: How to create lineEdits as an array?
Quote:
Originally Posted by
Lesiok
Yes but not in Qt Creator.
You probably meant "not in Qt Designer"
Cheers,
_
Re: How to create lineEdits as an array?
Quote:
Originally Posted by
anda_skoa
You probably meant "not in Qt Designer"
Cheers,
_
Of course ;)
Re: How to create lineEdits as an array?
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
Re: How to create lineEdits as an array?
I guess you are trying to do this.
Code:
#include <QApplication>
#include <QDialog>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QList>
int main(int argc, char *argv[])
{
QList<QLineEdit *> qle_list;
for(int i = 0; i < 10; ++i)
{
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
Code:
for-loop: qle_list.at(i)->objectName();
Re: How to create lineEdits as an array?
Quote:
Originally Posted by
Wolletje
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.
Re: How to create lineEdits as an array?
If you want to create the buttons in Qt Designer, you can still retrieve them into a list using QObject::findChildren().
Cheers,
_
Re: How to create lineEdits as an array?
Quote:
Originally Posted by
Wolletje
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):
Code:
for (int i = 0; i < texts.size(); ++i) {
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}