Results 1 to 4 of 4

Thread: Clear button in line edit

  1. #1
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Clear button in line edit

    In KDE every line edit has a clear button that appears when there is text.

    How do I do that? Preferably something that doesn't require KDE.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Clear button in line edit

    It's pure Qt the KDE people use. Just have a look into their sources and see how they make it. Or have a look at wysotas wwWidget. He also provide such a line edit.

    In theory it is simply a button which overlays the line edit.

  3. #3
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Clear button in line edit

    I found this, but the links to the source code are broken:

    http://labs.trolltech.com/blogs/2007...-clear-button/

  4. #4
    Join Date
    Jun 2010
    Posts
    142
    Thanks
    11
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Clear button in line edit

    I figured out how to get a button into QLineEdit:

    Qt Code:
    1. EnhancedLineEdit::EnhancedLineEdit(QWidget *parent) :
    2. QLineEdit(parent)
    3. {
    4. QHBoxLayout *layout = new QHBoxLayout(this);
    5. setLayout(layout);
    6. layout->addStretch();
    7.  
    8. QPushButton *clearButton = new QPushButton(this);
    9. layout->addWidget(clearButton);
    10. connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
    11.  
    12. clearButton->setFlat(true);
    13. clearButton->setCursor(QCursor(Qt::ArrowCursor));
    14. clearButton->setIcon(QIcon("/usr/share/oxygen/icons/16x16/actions/edit-clear-locationbar-rtl.png"));
    15. }
    To copy to clipboard, switch view to plain text mode 

    Problem is that it's too wide, and for some reason the icon does not show in the button. Also, how do I give it a standard themeable icon, such as perhaps the "Cancel" or "Close Tab" icon?

    This also gave me a cool idea: Integrate a "Go" button into the QLineEdit that would emit a returnPressed() signal! That way you don't have to manage two widgets and two signal/slot connections for many QLineEdit applications.

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 20:06
  2. clear button for line edit
    By jayreddy in forum Qt Programming
    Replies: 6
    Last Post: 3rd December 2009, 05:36
  3. QLineEdit: with builtin clear button, how to
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 5th May 2009, 21:05
  4. Line edit and push button
    By dela in forum Newbie
    Replies: 1
    Last Post: 10th December 2008, 17:10
  5. Replies: 8
    Last Post: 15th May 2007, 10:21

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.