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. Highlighting entire string
Forum Updated to NodeBB v4.3 + New Features

Highlighting entire string

Scheduled Pinned Locked Moved General and Desktop
12 Posts 2 Posters 2.6k Views 2 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.
  • J Offline
    J Offline
    joeydonovan4
    wrote on last edited by
    #1

    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.

    simowS 1 Reply Last reply
    0
    • J joeydonovan4

      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.

      simowS Offline
      simowS Offline
      simow
      wrote on last edited by
      #2

      @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?

      Let's Try To Negotiate!

      J 1 Reply Last reply
      0
      • simowS simow

        @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?

        J Offline
        J Offline
        joeydonovan4
        wrote on last edited by
        #3

        @simow selecting the text contained in the text box.

        simowS 1 Reply Last reply
        0
        • J joeydonovan4

          @simow selecting the text contained in the text box.

          simowS Offline
          simowS Offline
          simow
          wrote on last edited by
          #4

          @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.

          Let's Try To Negotiate!

          J 1 Reply Last reply
          0
          • simowS simow

            @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.

            J Offline
            J Offline
            joeydonovan4
            wrote on last edited by
            #5

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

            simowS 1 Reply Last reply
            0
            • J joeydonovan4

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

              simowS Offline
              simowS Offline
              simow
              wrote on last edited by
              #6

              @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.

              Let's Try To Negotiate!

              J 1 Reply Last reply
              0
              • simowS simow

                @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.

                J Offline
                J Offline
                joeydonovan4
                wrote on last edited by
                #7

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

                simowS 1 Reply Last reply
                0
                • J joeydonovan4

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

                  simowS Offline
                  simowS Offline
                  simow
                  wrote on last edited by
                  #8

                  @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 {
                    public:
                      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 ) {
                    QLineEdit::focusInEvent(e);
                    // home(false);
                    selectAll();
                  }
                  

                  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().

                  Let's Try To Negotiate!

                  J 1 Reply Last reply
                  0
                  • simowS simow

                    @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 {
                      public:
                        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 ) {
                      QLineEdit::focusInEvent(e);
                      // home(false);
                      selectAll();
                    }
                    

                    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().

                    J Offline
                    J Offline
                    joeydonovan4
                    wrote on last edited by
                    #9

                    @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.

                    simowS 1 Reply Last reply
                    0
                    • J joeydonovan4

                      @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.

                      simowS Offline
                      simowS Offline
                      simow
                      wrote on last edited by simow
                      #10

                      @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 http://www.learncpp.com/cpp-tutorial/111-introduction-to-inheritance/ for further information.

                      Let's Try To Negotiate!

                      J 1 Reply Last reply
                      0
                      • simowS simow

                        @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 http://www.learncpp.com/cpp-tutorial/111-introduction-to-inheritance/ for further information.

                        J Offline
                        J Offline
                        joeydonovan4
                        wrote on last edited by
                        #11

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

                        simowS 1 Reply Last reply
                        0
                        • J joeydonovan4

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

                          simowS Offline
                          simowS Offline
                          simow
                          wrote on last edited by
                          #12

                          @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.

                          Let's Try To Negotiate!

                          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