Results 1 to 5 of 5

Thread: HOWTO: make a QLineEdit select all the text when clicked on?

  1. #1
    Join Date
    Jun 2010
    Location
    Cincinnati, Ohio, USA
    Posts
    92
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question HOWTO: make a QLineEdit select all the text when clicked on?

    On both the QEvent::Enter and QEvent::FocusIn of the QLineEdit, I am calling selectAll(), but it isn't working as desired...

    Everything gets selected ( because of QEvent::FocusIn, I think), but on the mouse down or something the selectAll is lost.

    Any thoughts on how to safely do a selectAll so I can simply type over the existing value?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: HOWTO: make a QLineEdit select all the text when clicked on?

    Qt Code:
    1. connect(this, SIGNAL(clicked()), this, SLOT(selectAll()));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HOWTO: make a QLineEdit select all the text when clicked on?

    Does QLineEdit have a clicked() signal

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: HOWTO: make a QLineEdit select all the text when clicked on?

    Apparently no :-)
    Monday morning I guess. I assumed all widgets had a clicked signal. Why don't they?

    Anyway, here's the signal:
    Qt Code:
    1. class MyLineEdit : public QLineEdit
    2. {
    3. ...
    4. signals:
    5. void clicked();
    6. ...
    7. };
    8.  
    9. // Edit: or mouseReleaseEvent(...) according to your taste
    10. void MyLineEdit::mousePressEvent(...)
    11. {
    12. emit clicked();
    13. }
    To copy to clipboard, switch view to plain text mode 

    I think, but I guess I need to get some more cafeine to wake up.

  5. #5
    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: HOWTO: make a QLineEdit select all the text when clicked on?

    Quote Originally Posted by scarleton View Post
    but on the mouse down or something the selectAll is lost.
    To prevent such behaviour, you'd have to filter out all unwanted events. But then you won't be able to do things as selecting parts of the input from the widget with the mouse and stuff. I think that what you really want is something like this:

    Qt Code:
    1. class MagicLineEdit : public QLineEdit {
    2. public:
    3. MagicLineEdit(QWidget *parent = 0) : QLineEdit(parent), m_firstChar(false) {}
    4. protected:
    5. void focusInEvent(QFocusEvent *e) {
    6. m_firstChar = true;
    7. QLineEdit::focusInEvent(e);
    8. }
    9. void keyPressEvent(QKeyEvent *ke) {
    10. if(m_firstChar /* && !ke->text().isEmpty() */) { // the commented part is optional
    11. clear();
    12. m_firstChar = false;
    13. }
    14. QLineEdit::keyPressEvent(ke);
    15. }
    16. private:
    17. bool m_firstChar;
    18. };
    To copy to clipboard, switch view to plain text mode 
    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.


Similar Threads

  1. clicked() signal for QLineEdit
    By smarinr in forum Qt Programming
    Replies: 10
    Last Post: 26th June 2019, 16:35
  2. Replies: 5
    Last Post: 10th December 2010, 19:24
  3. Howto select/show a QWebElement in QWebview
    By nightghost in forum Qt Programming
    Replies: 1
    Last Post: 2nd September 2010, 12:51
  4. Replies: 1
    Last Post: 6th November 2009, 10:03
  5. How to make some part of text underlined in QLineEdit?
    By blonde in forum Qt Programming
    Replies: 1
    Last Post: 6th November 2009, 09:43

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.