Results 1 to 10 of 10

Thread: how to add and delete the text from dynamically created line edits

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to add and delete the text from dynamically created line edits

    Hi,

    My question refers to this thread created in 2011....
    http://www.qtforum.org/article/36821...namically.html

    I would like to modify it so the text can also be deleted with a delete button. I want to add all the text that gets inputted to a database but I want to allow the user to also delete a line input they wish to change.

    Here's what I have so far, it adds them to the list widget but with a lot of spacing for some reason...
    Qt Code:
    1. void userDialog::on_pushButton_3_clicked()
    2. {
    3. static int LayoutCount;
    4.  
    5. QLineEdit *lineEdit = new QLineEdit;
    6.  
    7. ui->gridLayout->addWidget( lineEdit,LayoutCount,0 );
    8.  
    9. int iCount = ui->gridLayout->count(); //Total no of LineEdit added on gridLayout dynamically
    10. QString str;
    11. for(int i = 0; i < iCount; i++)
    12. {
    13. QLayoutItem* pLine = ui->gridLayout->itemAt(i);
    14. QLineEdit* pLineEdit = (QLineEdit*)pLine->widget();
    15. str = pLineEdit->text();
    16. ui->listWidget->addItem(str);
    17.  
    18. qDebug() << str;
    19. }
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    Plz help...

  2. #2
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: how to add and delete the text from dynamically created line edits

    just add this to your onbuttonclicked function
    lineEdit->clear();
    or
    ui->lineEdit->clear();
    if you are using the designer ui form

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to add and delete the text from dynamically created line edits

    All items in a QLayout may not be QLineEdits (not event QWidgets), there are some layout space management items (which are QWidgets/QLineEdits). You need to take text() only from valid QLineEdit items, also I am wondring why does you program does not crash !

    Do something like this
    Qt Code:
    1. for(int i = 0; i < iCount; i++)
    2. {
    3. QLayoutItem* pLine = ui->gridLayout->itemAt(i);
    4. QLineEdit* pLineEdit = dynamic_cast<QLineEdit*>(pLine->widget()); //<<<<<<<<<<<<<<<<<<<
    5.  
    6. if(pLineEdit != 0) //<<<<<<<<<<<<<<<<<<<
    7. {
    8. str = pLineEdit->text();
    9. ui->listWidget->addItem(str);
    10. }
    11. qDebug() << str;
    12. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. The following user says thank you to Santosh Reddy for this useful post:

    Cyrebo (29th March 2013)

  5. #4
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to add and delete the text from dynamically created line edits

    [QUOTE=Santosh Reddy;241542]All items in a QLayout may not be QLineEdits (not event QWidgets), there are some layout space management items (which are QWidgets/QLineEdits). You need to take text() only from valid QLineEdit items, also I am wondring why does you program does not crash !

    Do something like this
    Qt Code:
    1. for(int i = 0; i < iCount; i++)
    2. {
    3. QLayoutItem* pLine = ui->gridLayout->itemAt(i);
    4. QLineEdit* pLineEdit = dynamic_cast<QLineEdit*>(pLine->widget()); //<<<<<<<<<<<<<<<<<<<
    5.  
    6. if(pLineEdit != 0) //<<<<<<<<<<<<<<<<<<<
    7. {
    8. str = pLineEdit->text();
    9. ui->listWidget->addItem(str);
    10. }
    11. qDebug() << str;
    12. }
    To copy to clipboard, switch view to plain text mode 
    [/QUxtOTE]

    Thanks but this does not solve my problem. The qlistwidget still adds extra spacing inbetween each text. This is a problem because if i want to read all the text in, I would be reading some spacing. How do i dynamically delete the text that was added? Right now the program is storing the text in a variable called str but I want to delete it from the qlistwidget and later read the text values in from there.

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to add and delete the text from dynamically created line edits

    How do i dynamically delete the text that was added?
    From where you want to delete text?

    Right now the program is storing the text in a variable called str but I want to delete it from the qlistwidget and later read the text values in from there.
    It is not clear yet, do want to clear the QListWidget ?, and later read text from where?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. The following user says thank you to Santosh Reddy for this useful post:

    Cyrebo (29th March 2013)

  8. #6
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to add and delete the text from dynamically created line edits

    I mean I have an add button function which allows for dynamically adding text to the list widget but I want to able to delete the text from the widget. The program saves the text entered at the point of entry by the user to a var called str. I want to alter this so that the text is saved in list widget then when the user clicks another button(Ok), each text is saved individually. I need this to save the text to a database table.

  9. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to add and delete the text from dynamically created line edits

    If you dynamically create widget that you would want to interact with later on, the best way is always to keep their pointers in an appropriately typed container.

    E.g. QList<QLineEdit*>, etc

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    Cyrebo (29th March 2013)

  11. #8
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to add and delete the text from dynamically created line edits

    Please answer my earlier questions, which will help me understand what you want
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  12. #9
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to add and delete the text from dynamically created line edits

    Quote Originally Posted by anda_skoa View Post
    If you dynamically create widget that you would want to interact with later on, the best way is always to keep their pointers in an appropriately typed container.

    E.g. QList<QLineEdit*>, etc
    _
    Thanks for suggestion but how do you do this calling the pointer from outside where you declared it?

    E.g. I have a declared it to show on runtime but I also want to take in the user input from it on the click of a button which inside another public/private slot. How do I call the pointer from there to get the value from the pointer object such as qlinedit?


    Added after 6 minutes:


    Quote Originally Posted by Santosh Reddy View Post
    Please answer my earlier questions, which will help me understand what you want
    My reply to anda skoa is pretty much what I want to accomplish as far as reading and writing goes.
    Last edited by Cyrebo; 30th March 2013 at 16:44.

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

    Default Re: how to add and delete the text from dynamically created line edits

    Quote Originally Posted by Cyrebo View Post
    Thanks for suggestion but how do you do this calling the pointer from outside where you declared it?

    E.g. I have a declared it to show on runtime but I also want to take in the user input from it on the click of a button which inside another public/private slot. How do I call the pointer from there to get the value from the pointer object such as qlinedit?
    I am not entirely sure what you mean.
    First you would have a QList<QLineEdit*> member in your class. When you create line edits dynamically, you add them to the list and can then iterate over that list or use index based access anywhere in your instance.

    Cheers,
    _

Similar Threads

  1. Accessing Dynamically created Checkboxe
    By premroxx in forum Newbie
    Replies: 1
    Last Post: 6th November 2012, 07:14
  2. Replies: 9
    Last Post: 2nd November 2011, 09:12
  3. Replies: 12
    Last Post: 24th October 2011, 07:56
  4. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09:40
  5. Replies: 0
    Last Post: 6th August 2010, 11:01

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.