Important: Please read the Qt Code of Conduct -

Highlighting entire string

  • Hi,

    Need to find Qt functionalities that will highlight an entire string of characters if the application or keyboard sets focus to an edit control (text box). I've been struggling to find a Qt function that will allow highlight strings to happen. Could use some help.

  • @joeydonovan4 Hi, one question: What do you exactly mean by "highlight an entire string of characters"? Is it selecting the text contained in the text box or is it referring to the style? That is: change background/foreground color while focused?

  • @simow selecting the text contained in the text box.

  • @joeydonovan4 Hm. What system are you running? Because that is the default behaviour on mine (Linux). When I tab through different QLineEdits, the entire text is selected each time.

  • @simow Seems to just work with textbox->selectAll();

  • @joeydonovan4 Yes, that's true. But I think you need to do a little more if you want to selectAll() after each focus change, e.g. sublassing QLineEdit and re-implement the focus event methods.

  • @simow Could you explain a little more? I'm new to this and could use some help

  • @joeydonovan4 Interesting. I cannot reproduce the behaviour on neither system (Linux, nor Windows) – in either case after pressing the tab key and a QLineEdit got the focus all text is automatically selected. So on first sight I don't see any point in implementing selectAll again. But maybe I'm missing something so I will post an example what one would have to do if the selectAll would not have been triggered automatically.
    One approach could be to subclass the QLineEdit:

    class MyLineEdit : public QLineEdit {
        MyLineEdit( QWidget *parent = 0 ) :
          QLineEdit(parent) {}
        void focusInEvent(QFocusEvent*);

    Here we overload the method focusInEvent in order to alter the behaviour:

    void MyLineEdit::focusInEvent( QFocusEvent *e ) {
      // home(false);

    In the implementation we first call the focusInEvent of the parent class to keep the default focus behaviour intact. Then you can either call selectAll() to to just that – or if you want to force the cursor to move always to the beginning of the text home().

  • @simow OK, but when I would call this function what type would the parameter 'e' have to be? The function that it's being called in takes the qt object as a parameter. Could I use that as the parameter of this overloaded function? I just don't have much experience with using events.

  • @joeydonovan4 In my example I created a new class MyLineEdit inheriting from QLineEdit and then overloaded the focusInEvent method. You never call this method explicitly. It is called every time the control gets the focus.
    This technique – class inheritance and method overloading – is not indigenous to Qt but C++. You may want to read for further information.

  • @simow the thing is that I do not want this to occur in every line edit, just a few specific ones in a dialog.

  • @joeydonovan4 Then just add a property like for example autoSelectAllOnFocus to the new subclassed MyLineEdit and then in the overloaded method you either call home() or selectAll() in dependence of the property.

Log in to reply