[Solved] Remove Text Selection when QLineEdit gets focus.



  • Hello All,

    I am using QT 5.3.2 in Fedora Core 13. There is a one QLineEdit present in the QWidget. Whenever i click on the QLineEdit, all the text present in it becomes selected with blue color highlight. Is there any way to remove this behavior?

    I dont want any of the text selection on focus of the QLine Edit.

    Kindly help.

    Thanks



  • did you try using deselect(..) API of QLineEdit ?



  • Yes i tried that as well.....but still the text is getting highlighted in blue color..


  • Lifetime Qt Champion

    Hi,

    You should subclass QLineEdit and re-implement focusInEvent.

    Something like

    @
    class MyLineEdit : public QLineEdit
    {
    protected:
    void focusInEvent(QFocusEvent *e)
    {
    QLineEdit::focusInEvent(e);
    deselect();
    }
    };
    @



  • Dear SGaist,

    Thanks for the reply. I tried the above code also as you suggested. But still it is not working.
    Should i try the same with signal slot mechanism?

    Kindly let me know if there is some other work around.

    Cheers
    Siddharth



  • Tried with signal slot mechanism also but no luck ..



  • The reasons SGaist suggestion might not work either
    when something else (not focusInEvent ) selects it or selectAll slot is executed through event loop.
    In such case instead of deselect() call you may try to emit the custom signal which you can connect to deselect slot with Qt::QueuedConnection connection type.

    Also I want to say that this is bug anyway. Selection should not be changed on focus in. And this was not happening in Qt 4.



  • Thanks all,

    Actually it is now working fine. Some other component was setting the
    @
    selectAll()
    @

    Because of that the behavior was not correct.

    Now all is OK

    Cheers!



  • Hi
    Trying similar functionality with QTableWidgetItem. I need a MS Excel like functionality. On pressing Alt + enter, The cursor should go o next line, and the text already entered should not be selected.

    QTableWidgetItem is not a sub class of QWidget, so how to approach?

    Thanks


Log in to reply
 

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