Results 1 to 8 of 8

Thread: clear all QLineEdit

  1. #1
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Cool clear all QLineEdit

    Hello!
    Is there any way to clean the QLineEdits with little code?
    Thanks.

  2. #2
    Join Date
    Apr 2008
    Location
    Pavlodar, Kazakhstan
    Posts
    22
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: clear all QLineEdit

    Have you tried lineEdit->setText("") ?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: clear all QLineEdit

    QLineEdit::clear()
    That question actually belongs into the Newbie forum and you should always take a look at the qt documentation before asking .
    If I misunderstood your question and you are speaking of Many QLineEditS that you want to clear at once, you should have a function that iterates through the list of QLineEdits and calls the mentioned function on them. Use findChildren for that:

    Qt Code:
    1. foreach(QLineEdit* le, p->findChildren<QLineEdit*>()) {
    2. le->clear();
    3. }
    To copy to clipboard, switch view to plain text mode 
    where p is the common parentwidget/ancestor of all the lineedits.
    Last edited by jpn; 13th May 2008 at 20:07. Reason: missing [code] tags

  4. The following 3 users say thank you to momesana for this useful post:

    fnmblot (14th May 2008), GokuH12 (22nd August 2008), jaca (13th May 2008)

  5. #4
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: clear all QLineEdit

    Yes, better use lineEdit.clear(). But I have many lineEdits to clear and the code would be great. I wanted to know if there is a loop for this.

  6. #5
    Join Date
    Dec 2007
    Location
    Brazil
    Posts
    61
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: clear all QLineEdit

    was easier to use without declaring P. It was thus:

    Qt Code:
    1. foreach(QLineEdit* le, findChildren<QLineEdit*>()) {
    2. le->clear();
    3. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 13th May 2008 at 20:07. Reason: missing [code] tags

  7. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: clear all QLineEdit

    Quote Originally Posted by jaca View Post
    was easier to use without declaring P.
    Yes, the code above works fine if all Line edits are children or descendants (in terms of parenthood, not inheritance) of the Widget you are calling that function from. In that case the implicit "this" corresponds to p as findChildren() is the same as this->findChildren(). However, if the line edits happen to be in a widget that is not a descendant/child of "this" then you must explicitly specify their ancestor/parent .

  8. #7
    Join Date
    Aug 2016
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: clear all QLineEdit

    foreach(QLineEdit* le, findChildren<QLineEdit*>()) {
    le->clear();
    }

    this code is working and thank you.
    but i have some QLineEdit I don't need to clear.
    what should be the code?

  9. #8
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: clear all QLineEdit

    QLineEdit::clear() is a slot,so you can connect an own-defined signal clearLineEdits to those line edits you want to be cleared at once:
    Qt Code:
    1. //prefferably in constructor:
    2. Class::Class()
    3. {
    4. connect(this, SIGNAL(clearLineEdits()), lineEdit_nr1, SLOT(clear()));
    5. .......
    6. connect(this, SIGNAL(clearLineEdits()), lineEdit_nrX, SLOT(clear()));
    7. }
    To copy to clipboard, switch view to plain text mode 
    and then if emit the signal somewhere in your code:
    Qt Code:
    1. emit clearLineEdits();
    To copy to clipboard, switch view to plain text mode 

    Why not simpy a function where you'd put all the line edits to clear?Because signal-slot approach is more flexible,as you can add/remove line edits during runtime.
    Last edited by MasterBLB; 5th August 2016 at 08:15.

Similar Threads

  1. how to get last character of QLineEdit
    By yagabey in forum Qt Programming
    Replies: 1
    Last Post: 5th January 2008, 17:38
  2. closeEditor() not always closing QLineEdit
    By mclark in forum Qt Programming
    Replies: 8
    Last Post: 27th November 2007, 02:44
  3. Problem Euro Sign with QLineEdit
    By Kubil in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2007, 05:56
  4. QValidator, regular expressions and QLineEdit
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 02:25
  5. a box around QLineEdit?
    By GreyGeek in forum Qt Tools
    Replies: 13
    Last Post: 8th February 2006, 16:40

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.