Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Have text in QLineEdit selected on edit start
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Have text in QLineEdit selected on edit start

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 10.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yanbellavance
    wrote on last edited by
    #1

    I need to have a QLineEdit's text be selected when it receives focus. Right now it is being done only when I tab into it, not when i click into it. i tried the selectAll() function but that did not work. I tested the selectAll() function in another scenario to verify its functionality and it does work so something is overiding me when and voiding my call to selectAll(). I suspect it is related to the fact that I am clicking but I have strongFocus setup. How can do this? I was calling the selectAll() function inside the focusInEvent:

    @void wvQlineEdit::focusInEvent ( QFocusEvent * event ) {

    this->setWriteEnable(false);
    oldVal=text();
    originalVal=text();
    selectAll();
    QLineEdit::focusInEvent(event);
    

    }@

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DenisKormalev
      wrote on last edited by
      #2

      As wild guess: have you tried to call QLineEdit::focusInEvent(event); before selectAll(); ?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        The text is selected in the focusInEvent, but immediately gets deselected by the following mousePressEvent hat is delivered to the line edit.

        This code works for me:

        @

        class wvQlineEdit : public QLineEdit
        {
        Q_OBJECT
        public:
        wvQlineEdit(QWidget *parent);
        virtual ~wvQlineEdit() {}

        protected:
        void focusInEvent(QFocusEvent *e);
        void mousePressEvent(QMouseEvent *me);

        bool _selectOnMousePress;
        

        };

        wvQlineEdit::wvQlineEdit(QWidget *parent)
        : QLineEdit(parent)
        {
        _selectOnMousePress = false;
        }

        void wvQlineEdit::focusInEvent(QFocusEvent *e)
        {
        QLineEdit::focusInEvent(e);
        selectAll();
        _selectOnMousePress = true;
        }

        void wvQlineEdit::mousePressEvent(QMouseEvent *me)
        {
        QLineEdit::mousePressEvent(me);
        if(_selectOnMousePress) {
        selectAll();
        _selectOnMousePress = false;
        }
        qDebug() << selectedText();

        }
        @

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved