Results 1 to 4 of 4

Thread: Event Filter doesnt work on Drop Event

  1. #1
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Event Filter doesnt work on Drop Event

    Hello Forum,

    I had a QPlainTextEdit and I wanted to change the Drop behavior. When dropped a file with a certain ending instead of pasting the name of the files location, the file should be loaded and the content pasted into the editor.
    I tried to avoid subclassing the QPlainTextEdit and installed an eventfilter in my MainWindow that should take care of the editors DropEvents. But this didnt work and dispite of receiving a lot of events (MouseMove, Enter, Leave, etc.) I could not receive a Drop Event.

    So I subclassed the PlainTextEdit and everything worked fine. But the Question ist still bothering me. So I played around with it.

    I have this Test-EventFilter in my Mainwindow.cpp

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if(obj == ui->plainTextEditGCode){
    4. if(event->type() == QEvent::Drop){
    5. qDebug()<<"Drop Event Filterd in MainWindow class";
    6. return true;
    7. }else{
    8. qDebug()<<"Some other Event Filterd in MainWindow class";
    9. }
    10. }
    11. return false;
    12. }
    To copy to clipboard, switch view to plain text mode 

    It is set in the MainWindows constructor
    Qt Code:
    1. ui->plainTextEditGCode->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 

    In the editors class (inherits QPlainTextEdit) I implemented the DropEvent method

    Qt Code:
    1. void CodeEditor::dropEvent(QDropEvent *event)
    2. void CodeEditor::dropEvent(QDropEvent *event)
    3. {
    4. qDebug()<<"Drop Event in CodeEditor class";
    5. QPlainTextEdit::dropEvent(event); //seems to be necessary to call baseclass method to make it work poperly
    6. if(event->mimeData()->hasUrls()){
    7. //here the file loading stuff is done
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    So when I run this code, and drop a file in the editor I get a debug output like this:

    Some other Event Filterd in MainWindow class
    Some other Event Filterd in MainWindow class
    Some other Event Filterd in MainWindow class
    Drop Event in CodeEditor class
    Some other Event Filterd in MainWindow class
    So apparently the event filter seems to work (receives events) but not for DropEvents. Is there anything special about them that makes them behave differently?

    Greetings

    DNW

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Event Filter doesnt work on Drop Event

    What could happen is that the drop event is not actually sent to the plain text edit but its viewport widget.

    You could check by installing the event filter on ui->plainTextEditGCode->viewport()

    Cheers,
    _

  3. #3
    Join Date
    Oct 2015
    Posts
    2
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Event Filter doesnt work on Drop Event

    Thank you, that was it.

    I expanded my EventFilter and installed it in the editors viewport, too.

    Qt Code:
    1. bool MainWindow::eventFilter(QObject *obj, QEvent *event)
    2. {
    3. if(obj == ui->plainTextEditGCode->viewport()){
    4. if(event->type() == QEvent::Drop){
    5. qDebug()<<"Drop Event filterd from Viewport in MainWindow class";
    6. return true;
    7. }else{
    8. qDebug()<<"Some other Event filterd from Viewport in MainWindow class";
    9. }
    10.  
    11. }
    12.  
    13. if(obj == ui->plainTextEditGCode){
    14. if(event->type() == QEvent::Drop){
    15. qDebug()<<"Drop Event filterd in MainWindow class";
    16. return true;
    17. }else{
    18. qDebug()<<"Some other Event filterd in MainWindow class";
    19. }
    20. }
    21. return false;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Now my debug output looks like this:

    Some other Event filterd from Viewport in MainWindow class
    Drop Event Filterd from Viewport in MainWindow class
    Some other Event filterd in MainWindow class
    Some other Event filterd in MainWindow class
    It seems like there a some events that are transfered to the editors viewport instead of the editor. But I am still a bit wondering why I can hande the Event in the subclass then. So it feels like an inconsitancy in the interface. The Dropevent is somehow part of the class but an EventFilter on the entire class is not reaching it. So maybe the event in the viewport is triggerd and it calls the editors event function without using the part of the event system that is handeling the filters. Am I right?

    @Mods: can I changes the threads title to add a [SOLVED] in front of it?
    Last edited by DNW; 25th October 2015 at 02:47.

  4. The following user says thank you to DNW for this useful post:

    schollii (13th December 2016)

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Event Filter doesnt work on Drop Event

    Quote Originally Posted by DNW View Post
    So maybe the event in the viewport is triggerd and it calls the editors event function without using the part of the event system that is handeling the filters. Am I right?
    Easy enough to check

    http://code.woboq.org/qt5/qtbase/src...portEP7QWidget

    The scroll area sets an event filter on its viewport.

    Btw, in your case subclassing the plain text edit is probably the more robust way. Drop events are usually processed by the widget the drop occurs on.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    DNW (25th October 2015)

Similar Threads

  1. filter mouse event
    By sajis997 in forum Qt Quick
    Replies: 10
    Last Post: 16th February 2015, 08:26
  2. Event Filter doesnt work as expected
    By Hossein in forum Newbie
    Replies: 1
    Last Post: 12th July 2013, 22:01
  3. Event filter: very confused about how they work
    By papillon in forum Qt Programming
    Replies: 8
    Last Post: 20th November 2011, 01:46
  4. Event filter question
    By d_stranz in forum Qt Programming
    Replies: 7
    Last Post: 8th July 2011, 00:08
  5. qt4.5, assert in timer event doesnt stop all timers
    By roybj in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2011, 13:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.