Results 1 to 11 of 11

Thread: How to create lineEdits as an array?

  1. #1
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,461
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create lineEdits as an array?

    Yes but not in Qt Creator. You must do this programmatically, more or less like this :
    Qt Code:
    1. QList<QLineEdit*> qle_list;
    2. for( int i=0; i < 100; i++ )
    3. qle_list.append(new QLineEdit());
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,461
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create lineEdits as an array?

    In attachment simple example.
    Attached Files Attached Files

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create lineEdits as an array?

    Quote Originally Posted by Lesiok View Post
    Yes but not in Qt Creator.
    You probably meant "not in Qt Designer"

    Cheers,
    _

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,461
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create lineEdits as an array?

    Quote Originally Posted by anda_skoa View Post
    You probably meant "not in Qt Designer"

    Cheers,
    _
    Of course

  7. #7
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default 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

  8. #8
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    248
    Thanks
    91
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to create lineEdits as an array?

    I guess you are trying to do this.

    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3. #include <QLineEdit>
    4. #include <QVBoxLayout>
    5. #include <QList>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication a(argc, argv);
    10.  
    11. QVBoxLayout * vLayout = new QVBoxLayout(&w);
    12. QList<QLineEdit *> qle_list;
    13.  
    14. for(int i = 0; i < 10; ++i)
    15. {
    16. qle_list.append(new QLineEdit());
    17. qle_list.at(i)->setObjectName(QString("lineEdit%1").arg(i)); // It's easier to recognize an object by name
    18. vLayout->addWidget(qle_list.at(i));
    19. }
    20. w.setLayout(vLayout);
    21. w.show();
    22.  
    23. return a.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 

    For example, you can get the object name by
    Qt Code:
    1. for-loop: qle_list.at(i)->objectName();
    To copy to clipboard, switch view to plain text mode 
    Last edited by rawfool; 17th November 2016 at 09:00.

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,461
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to create lineEdits as an array?

    Quote Originally Posted by Wolletje View Post
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  11. #11
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to create lineEdits as an array?

    Quote Originally Posted by Wolletje View Post
    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):
    Qt Code:
    1. QGridLayout *gridLayout = new QGridLayout;
    2. for (int i = 0; i < texts.size(); ++i) {
    3. QPushButton *button = new QPushButton(texts[i]);
    4. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    5. signalMapper->setMapping(button, texts[i]);
    6. gridLayout->addWidget(button, i / 3, i % 3);
    7. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to create a 2dimensional array in Qt
    By athulms in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2011, 07:45
  2. Qt Designer How to create an array of objects??
    By dominate in forum Qt Tools
    Replies: 2
    Last Post: 22nd January 2011, 16:34
  3. How to Create control Array in QT4
    By umulingu in forum Qt Programming
    Replies: 4
    Last Post: 8th October 2009, 15:31
  4. how can i create array of lineedits?
    By BalaQT in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2009, 08:17
  5. Create an array of Widgets
    By gt.beta2 in forum Qt Tools
    Replies: 4
    Last Post: 25th February 2009, 19:44

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.