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] Remove Text Selection when QLineEdit gets focus.
Forum Updated to NodeBB v4.3 + New Features

[Solved] Remove Text Selection when QLineEdit gets focus.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 9.1k 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.
  • S Offline
    S Offline
    Sidii
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Sidii
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You should subclass QLineEdit and re-implement focusInEvent.

          Something like

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • S Offline
            S Offline
            Sidii
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sidii
              wrote on last edited by
              #6

              Tried with signal slot mechanism also but no luck ..

              1 Reply Last reply
              0
              • A Offline
                A Offline
                alex_malyu
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sidii
                  wrote on last edited by
                  #8

                  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!

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Akashh
                    wrote on last edited by
                    #9

                    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

                    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