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. How to select the Values from a QLineEdit using the Mouse Press Event?
Forum Updated to NodeBB v4.3 + New Features

How to select the Values from a QLineEdit using the Mouse Press Event?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 5 Posters 8.0k 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.
  • J Offline
    J Offline
    JoyRider
    wrote on last edited by
    #1

    I'm a new bie to Qt. Plz Help Me to solve my doubt. I have a LineEdit With a Text in it. And if i click somewhere in the LineEdit, the text value should be selected and if i enter any other characters in it, the old selected value has to be erased and the new one should be placed in the LineEdit.

    I Checked for the LineEdit Signal Clicked, but it is not there for a LineEdit. Then i came to know that i have to subclass my LineEdit and using the Focus Event i can get it. But here i don't want to Subclass my LineEdit. Can anyone please tell me how to select the entire text of a line edit using mouse click without subclassing the QLineEdit.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BelenMuñoz
      wrote on last edited by
      #2

      Hello!!
      Maybe I didn't understand your problem, but in my last project, I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
      After that, if I enter any character, the selected text is erased.

      Me casé con un enano pa jartarme de reí.

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        You have to subclass QLineEdit and reimplement a (single) click event that calls selectAll

        @class MyLineEdit : public QLineEdit
        {
        Q_OBJECT
        public:
        explicit MyLineEdit(QWidget *parent = 0);

        protected:
        void mouseReleaseEvent(QMouseEvent *);

        };@

        and in the cpp...

        @void MyLineEdit::mouseReleaseEvent(QMouseEvent *)
        {
        selectAll();
        }
        @

        you can do it with the press event too, but since you can press on the line edit and move away from it and then release outside, I think it is better to use the release event to make sure the user wants to do a complete click on the line edit

        1 Reply Last reply
        0
        • J Offline
          J Offline
          JoyRider
          wrote on last edited by
          #4

          [quote author="Belencina1" date="1358171882"]
          I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
          After that, if I enter any character, the selected text is erased.[/quote]

          How did u did that? Did u sub classed your Line Edit. Or you tried with any other ways?

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JoyRider
            wrote on last edited by
            #5

            [quote author="utcenter" date="1358175843"] you can do it with the press event too, but since you can press on the line edit and move away from it and then release outside, I think it is better to use the release event to make sure the user wants to do a complete click on the line edit[/quote]

            Yup, I tried using Event Filters. In that i tried,

            @if(keyEvent->key() == QEvent::MouseButtonPress)
            {
            qDebug() << "Line Edit Value Selected"; // Just tried using a qDebug. But it didn't works.
            }@

            But i couldn't get the proper solution. Can u plz tell me where am going wrong,

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on last edited by
              #6

              I think keyEvent is only concerned with with keyboard events, otherwise you should see your message.

              QEvent has a mouse button press, but QKeyEvent doesn't, so they cannot be equal as you test.

              The QLineEdit, as a subclass of QWidget, has a mousePress and mouseRelease event, it does not have clicked() signal.

              So the way to achieve what you want is the method I showed you above. If you want, you can replace the Release with a Press event, it is still guaranteed to work.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Macro
                wrote on last edited by
                #7

                [quote author="Joy Rider" date="1358194278"]
                Yup, I tried using Event Filters. In that i tried,

                @if(keyEvent->key() == QEvent::MouseButtonPress)
                {
                qDebug() << "Line Edit Value Selected"; // Just tried using a qDebug. But it didn't works.
                }@

                But i couldn't get the proper solution. Can u plz tell me where am going wrong,
                [/quote]

                Using Event Filters, It will Work Fine. Only thing you have to do is, a small change in your if condition to if(event->type == QEvent::MouseButtonRelease)

                Here's the code for selecting the QLineEdit Text using event filter.

                @if(event->type() == QEvent::MouseButtonRelease)
                {
                q_LineEdit->selectAll();
                }
                @

                Thanks & Regards...

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Macro
                  wrote on last edited by
                  #8

                  [quote author="Belencina1" date="1358171882"]
                  I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
                  After that, if I enter any character, the selected text is erased.[/quote]

                  That's Right.... :-)

                  Thanks & Regards

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on last edited by
                    #9

                    bq. If you are not subclassing QLineEdit then make sure that you use lineEdit->installEventFilter(this) and then in the eventFilter() function of your qwidget/qmainwindow you check for the target if (target == lineEdit ) then perform the valid operations.

                    P.S : not required for the current topic

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BelenMuñoz
                      wrote on last edited by
                      #10

                      [quote author="Joy Rider" date="1358193837"]
                      [quote author="Belencina1" date="1358171882"]
                      I use QLineEdit and when I double click on it, the text is automatically selected, one double click for the nearest word and 2 double click for the whole line.
                      After that, if I enter any character, the selected text is erased.[/quote]

                      How did u did that? Did u sub classed your Line Edit. Or you tried with any other ways?

                      [/quote]

                      It worked for me this way automatically, without subclassing or using any option.

                      Me casé con un enano pa jartarme de reí.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sam
                        wrote on last edited by
                        #11

                        [quote author="Joy Rider" date="1358170755"]I'm a new bie to Qt. Plz Help Me to solve my doubt. I have a LineEdit With a Text in it. And if i click somewhere in the LineEdit, the text value should be selected and if i enter any other characters in it, the old selected value has to be erased and the new one should be placed in the LineEdit.

                        I Checked for the LineEdit Signal Clicked, but it is not there for a LineEdit. Then i came to know that i have to subclass my LineEdit and using the Focus Event i can get it. But here i don't want to Subclass my LineEdit. Can anyone please tell me how to select the entire text of a line edit using mouse click without subclassing the QLineEdit. [/quote]

                        You dont need to subclass anything , The double click text selection property is by default available with the lineEdit. What steps are you following ?

                        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