[SOLVED] Have text in QLineEdit selected on edit start



  • 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);
    

    }@



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



  • 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();

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.