Results 1 to 5 of 5

Thread: selectAll in QLineEdit does not work

  1. #1
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default selectAll in QLineEdit does not work

    I have subclassed QLineEdit in order to handle a focus event, and I would like all existing text to be highlighted as soon as I click in the textbox. The selectAll() method does not work. What am I missing?

    Qt Code:
    1. // --------------------------------------------------------------------
    2. class MyLineEdit : public QLineEdit
    3. {
    4. Q_OBJECT
    5.  
    6. protected:
    7. void focusInEvent(QFocusEvent *);
    8.  
    9. };
    10.  
    11. void MyLineEdit::focusInEvent(QFocusEvent *e) {
    12.  
    13. this->selectAll(); // THIS DOESN'T WORK
    14. //this->clear(); // <-- THIS WORKS!
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: selectAll in QLineEdit does not work

    It most likely is working. What your getting is that you are selecting all the text, and then click a character in the box with your mouse, and so clearing the selection. Try it yourself - select all the text and then click in the box with your mouse.

    One solution would be to wait until the mouse button is released before doing the selectAll. Another would be to use a timer.

  3. #3
    Join Date
    Sep 2009
    Posts
    41
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: selectAll in QLineEdit does not work

    Hmm, I didn't quite understand your answer. I must be missing something. Here's a complete example. I would like the text to be highlighted as soon as I click in the textbox.

    Qt Code:
    1. #ifndef MYLINEEDIT_H
    2. #define MYLINEEDIT_H
    3.  
    4. #include <QtGui>
    5.  
    6. class MyLineEdit : public QLineEdit
    7. {
    8. Q_OBJECT
    9.  
    10. protected:
    11. void focusInEvent(QFocusEvent *);
    12. };
    13.  
    14. #endif // MYLINEEDIT_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mylineedit.h"
    2.  
    3. void MyLineEdit::focusInEvent(QFocusEvent *) {
    4. //this->clear();
    5. this->selectAll();
    6. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mylineedit.h"
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QFrame *f = new QFrame();
    8. MyLineEdit *tmp = new MyLineEdit();
    9. tmp->setText("test this");
    10. QHBoxLayout *flayout = new QHBoxLayout(f);
    11. flayout->addWidget(tmp);
    12. f->setFocus();
    13. f->show();
    14. return app.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: selectAll in QLineEdit does not work

    I would surely call the base class implementation of the event handler as it does some useful things.

    Qt Code:
    1. void MyLineEdit::focusInEvent(QFocusEvent *e) {
    2. QLineEdit::focusInEvent(e);
    3. this->selectAll();
    4. }
    To copy to clipboard, switch view to plain text mode 

    If you want to select contents of the line edit upon receiving focus (and focusInEvent doesn't work, remember about the base class implementation call!) then connect QApplication::focusChanged() signal to a custom slot, check if the widget receiving the focus is your line edit and call selectAll() on it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2009
    Posts
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: selectAll in QLineEdit does not work

    Hi, in case someone comes across this thread, this solution worked for me:

    Qt Code:
    1. void MyCustomSpinBox::focusInEvent(QFocusEvent *event)
    2. {
    3. QTimer::singleShot(0, [this](){
    4. selectAll();
    5. });
    6. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to Michal Fapso for this useful post:

    Kryzon (30th September 2015)

Similar Threads

  1. Replies: 2
    Last Post: 13th December 2009, 21:27
  2. QLineEdit
    By rick_st3 in forum Newbie
    Replies: 1
    Last Post: 14th June 2008, 10:05
  3. QLineEdit selectAll() in eventFilter
    By mclark in forum Qt Programming
    Replies: 6
    Last Post: 1st February 2008, 09:13
  4. Replies: 3
    Last Post: 15th January 2008, 13:11
  5. SelectAll causes freeze
    By Khal Drogo in forum Qt Programming
    Replies: 20
    Last Post: 4th December 2007, 19:26

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.