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. Using an Eventfilter
Forum Updated to NodeBB v4.3 + New Features

Using an Eventfilter

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 4 Posters 6.0k Views 3 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.
  • Scott KriseS Scott Krise

    Well mpergand, you might be on to something.

    I changed:

    clickCatcher *clickCatcher = new clickCatcher(this);
    ui->reserveLineEdit->installEventFilter(clickCatcher);

    To:

    clickCatcher *ClickCatcher = new clickCatcher(this);
    ui->reserveLineEdit->installEventFilter(ClickCatcher);

    and that error message went away. But now I got a new error.

    "Undefined reference to 'clickCatcher::clickCatcher(QWidget*)"

    M Offline
    M Offline
    mpergand
    wrote on last edited by
    #10

    "Undefined reference to 'clickCatcher::clickCatcher(QWidget*)"

    unconstancy spotted ;)

    Well, you want to add some functionalities to QLineEdit, in OPP we do that by subclassing.
    In your subclass, override mouseDoubleClickEvent(QMouseEvent *event)
    You can do what ever you want from there, call special function, emit a signal, etc.

    Event filters can be powerfull , but look a little hacky for your use case, AMHO.

    1 Reply Last reply
    1
    • Scott KriseS Offline
      Scott KriseS Offline
      Scott Krise
      wrote on last edited by
      #11

      Do you have an example of a subclass that does something like Im trying to do that you could share? Im still stuck on the event filters at the moment and cant seem to get it to work.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #12
        class MyLineEdit : public QLineEdit
        {
            void mouseDoubleClickEvent(QMouseEvent *event)
            {
            qDebug()<<event;
            }
        
        };
        

        Log:

        QMouseEvent(MouseButtonDblClick, LeftButton, localPos=155,12, screenPos=797,323.082)
        
        1 Reply Last reply
        1
        • Scott KriseS Offline
          Scott KriseS Offline
          Scott Krise
          wrote on last edited by
          #13

          Ok...I haven't used subclass in years...refresh my memory.

          So, in your app, then instead of using QLineEdit for your fields, you use MyLineEdit? Then when you do, your customized logic for mouseDoubleClickEvent takes precedence over what QLineEdit does when it encounters a double click?

          So do you use QT Creator to design your forms? How do you get MyLineEdit to be an option when designing a form?

          And not sure what is meant by Log: in your earlier post?

          M 1 Reply Last reply
          0
          • Scott KriseS Scott Krise

            Ok...I haven't used subclass in years...refresh my memory.

            So, in your app, then instead of using QLineEdit for your fields, you use MyLineEdit? Then when you do, your customized logic for mouseDoubleClickEvent takes precedence over what QLineEdit does when it encounters a double click?

            So do you use QT Creator to design your forms? How do you get MyLineEdit to be an option when designing a form?

            And not sure what is meant by Log: in your earlier post?

            M Offline
            M Offline
            mpergand
            wrote on last edited by mpergand
            #14

            your customized logic for mouseDoubleClickEvent takes precedence over what QLineEdit does when it encounters a double click?

            You can look at some modifier keys, ALT,SHIFT etc
            Try to detect a tripple click (don't know how it works in Qt)

            [edit] obviously, you can call the base method to have the standard behavior:

            QLineEdit::mouseDoubleClickEvent(event);
            

            Maybe it would be simplier to use a context Menu, you can add your actions to the menu return by createStandardContextMenu()

            So do you use QT Creator to design your forms? How do you get MyLineEdit to be an option when designing a form?

            Look at "Promotion" in the Designer doc.

            And not sure what is meant by Log: in your earlier post?

            Logs are the messges you see in the Application output panel in QtCreator.

            1 Reply Last reply
            1
            • Scott KriseS Offline
              Scott KriseS Offline
              Scott Krise
              wrote on last edited by
              #15

              Thanks for the responses. Always wondered about "promotion"...I understand how thats used now, given our discussion.

              I like the "context menu" suggestion. Didn't know that existed, and I could see that being useful for either this situation or something else I might do in the future.

              Out of pure spite at this point, I still need to understand what I'm doing wrong with the event filter. At the end of the day on Friday, through much trial and error, I got my program to a point where I no longer am getting any errors when I compile, but it still doesn't do anything when I double click inside the field!

              I'll be able to work on it again tomorrow, but I'm nearly certain there is still an issue with clickCatcher.cpp. That was my first attempt at creating a program without a UI associated with it. I didn't have any documentation or models to follow, I just removed code that either failed during the compile, or just didn't make sense given no UI exists. Anyone see anything in the CPP i posted earlier that may explain why the "qDebug("Ate key press %d", keyEvent->key());" doesn't fire when start typing in that field? Once I get over that hurdle, with a small change to start capturing the double click event instead of "keypress", I should have what I set out to accomplish in the first place.

              Thanks!!

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mpergand
                wrote on last edited by mpergand
                #16

                Continuing with MyLineEdit subclass:

                class MyLineEdit : public QLineEdit
                {
                    public:
                    MyLineEdit(QWidget* parent=nullptr) : QLineEdit(parent)
                    {
                    installEventFilter(this);
                    }
                
                    void mouseDoubleClickEvent(QMouseEvent *event)
                    {
                    qDebug()<<event;
                    }
                
                    bool  eventFilter(QObject *obj, QEvent *event)
                    {
                    if( (dynamic_cast<QMouseEvent*>(event)) && (obj==this))
                        qDebug()<<event<<obj;
                
                    return false;
                    }
                
                };
                

                In the output panel I see:

                QMouseEvent(MouseMove, localPos=102,9, screenPos=744.082,320.039) QLineEdit(0x7fff5a830b60)
                QMouseEvent(MouseButtonPress, LeftButton, localPos=102,9, screenPos=744.082,320.039) QLineEdit(0x7fff5a830b60)
                QMouseEvent(MouseButtonRelease, LeftButton, localPos=102,9, screenPos=744.082,320.039) QLineEdit(0x7fff5a830b60)
                QMouseEvent(MouseButtonDblClick, LeftButton, localPos=102,9, screenPos=744.082,320.039) QLineEdit(0x7fff5a830b60)
                QMouseEvent(MouseButtonDblClick, LeftButton, localPos=102,9, screenPos=744.082,320.039)
                QMouseEvent(MouseButtonRelease, LeftButton, localPos=102,9, screenPos=744.082,320.039) QLineEdit(0x7fff5a830b60)
                

                There are two double click event one with no obj displayed (should be MyLineEdit)
                Don't know why ...

                Silly me, it is the QDebug message in mouseDoubleClickEvent :)

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

                  Hi,

                  Why are you installing an event filter on the widget since you are overloading the mouseDoubleClickEvent method ?

                  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
                  0
                  • Scott KriseS Offline
                    Scott KriseS Offline
                    Scott Krise
                    wrote on last edited by
                    #18

                    Sgaist...is that question to me or mpergand on his example?

                    I'm still lost...not getting the information I need.

                    So there isn't an easier way to capture a doubleclick event on a QLineEdit widget than doing an event filter?

                    If my widget were a qPushButton called processButton, I could capture it using...

                    void DeptScheduling::on_processButton_clicked()
                    {
                    }

                    There isn't just something like that to capture the doubleclick event on a qLineEdit widget?

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #19

                      Let's take a concrete example:

                      have the user double-click onto a date field and have the date replaced with todays date.

                      void mouseDoubleClickEvent(QMouseEvent *event)
                         {
                         //qDebug()<<event;
                         setText(QDate::currentDate().toString());
                         QLineEdit::mouseDoubleClickEvent(event);
                         }
                      

                      If you want to do this process outside the QLineEdit subclass, you have to emit a signal instead:

                      void mouseDoubleClickEvent(QMouseEvent *event)
                         {
                         //qDebug()<<event;
                         emit doubleClicked();
                         QLineEdit::mouseDoubleClickEvent(event);
                         }
                      

                      Look at the doc to know how to define a signal and emit/receive it.

                      1 Reply Last reply
                      2
                      • Scott KriseS Offline
                        Scott KriseS Offline
                        Scott Krise
                        wrote on last edited by
                        #20

                        Ok...I think this is where the disconnect is.

                        Is that a system wide change that would do that for all date fields in your app? Anytime I capture events, I do it on a field by field basis in the cpp of the form I'm working on.

                        Like in my example above:

                        void DeptScheduling::on_processButton_clicked()
                        {
                        }

                        Within my CPP called DeptScheduling, I have a field called processButton and the above is how I'd control what happens when the user clicks that button.

                        I want to control what happens on a double click on several different QLineEdit fields on a particular form, and each one will do something different. When the user double clicks a field called partReserve, I want to run a report showing all the parts in what we call "reserve'. If they doubleclick on a field called onHand, I might want to display a screen that show where all the lots and locations that total that onHand quantity.

                        The example you just provided seems to be system wide?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mpergand
                          wrote on last edited by mpergand
                          #21

                          In the case of a form, you simply identify the field by its name:
                          ui->field1
                          ui->filed2
                          etc

                          in your slot you can do:

                          void doubleClickOnField()
                          {
                          QObject* obj=sender();
                          
                          if(obj==ui->field1) { }
                          else if(obj==ui->field2 {}
                          ... etc
                          }
                          

                          Also, you will need to do a cast to retreive the particular object data. For ex:

                          QLineEdit* edit=qobject_cast<QLineEdit*>(sender());
                          
                          if(edit !=nullptr) // beware could be null
                          {
                          QString text=edit.tex();
                          }
                          
                          1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #22

                            Not trying to undermine your efforts but I think your users are going to be pretty surprised.

                            Personally, I don't expect anything other than text selection when I double click on a line edit type widget.

                            If a report shall be generate, I would rather have an explicit button for that.

                            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
                            3

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved