Results 1 to 13 of 13

Thread: Multiple QTextEdit instanciations and EventFilter

  1. #1

    Default Multiple QTextEdit instanciations and EventFilter

    Hello there !
    I've got a QPushButton linked to a slot that instantiates a QTextEdit each time you click on it (in fact, this slot only manages a boolean, my textEdit is instantiated in the mousePressEvent. The user is able to put it wherever he wants). My problem is that I need to use an eventFilter and it only works once. For example if I instantiate 3 textEdits, it'll work on only one of them (this is not related to the order in which they were instantiated)...
    I've got the same problem for a KeyPressEvent which is also in my eventFilter function. And in my eventFilter, my textEdit only detects right clicks, how can I do to make it detect the left click as well?
    (I've tried to do a static-cast of the event to QMouseEvent and make a condition on the button pressed but it's not working ).
    Here's my code:
    Qt Code:
    1. //Instantiation
    2. void ProjetGeometrie::mousePressEvent(QMouseEvent* evt)
    3. {
    4. if(clickTxt) //if the boolean is true (the slot connected to the pushButton manages that)
    5. {
    6. txt = new QTextEdit(this); //I instantiate my textEdit (declared in the .h)
    7. txt->installEventFilter(this);
    8. txt->move(evt->pos().x(), evt->pos().y()); //the textedit will be created at the position of the click
    9. setMouseTracking(true); //to allow the textEdit to be moved without having to keep the left button pressed
    10. txt->setContextMenuPolicy(Qt::NoContextMenu); //to disable the right-click context menu
    11. txt->show();
    12. }
    13. }
    14.  
    15. //eventFilter
    16. if(obj == txt && e->type() == QEvent::MouseButtonPress) //if the user clicks on the textEdit
    17. {
    18. QMouseEvent* me = static_cast <QMouseEvent*> (e);
    19. if(!txtSelectionne)//if the textEdit wasn't already "selected" then we select it
    20. txtSelectionne = true;
    21.  
    22. else //if it was, we "deselect" it
    23. txtSelectionne = false;
    24. return true;
    25. }
    26.  
    27. if(obj == txt && e->type() == QEvent::KeyPress)
    28. {
    29. QKeyEvent* ke = static_cast <QKeyEvent*> (e);
    30. if(ke->key() == Qt::Key::Key_Delete)
    31. {
    32. delete txt; //deletes the textEdit
    33. txt = NULL;
    34. return true;
    35. }
    36. }
    37.  
    38. return false;
    39.  
    40. //MouseMoveEvent
    41. void ProjetGeometrie::mouseMoveEvent(QMouseEvent* evt)
    42. {
    43. if(txtSelectionne) //if a textedit is selected, then we make it follow the mouse until the user clicks a second time (without having to keep the left button pressed)
    44. txt->move(evt->pos().x(), evt->pos().y());
    45.  
    46. update();
    47. }
    To copy to clipboard, switch view to plain text mode 


    To sum up, I've got two questions: - How to make my eventFilter work on every single textEdit created?
    - How to detect a left-button click on a textEdit?
    Thanks in advance for your replies,
    Cheers.
    Last edited by Yaoming; 13th February 2014 at 15:57.

  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: Multiple QTextEdit instanciations and EventFilter

    I am not sure what you are having problems with.

    You install the event filter on all text edits created in mousePressEvent.
    You only check events for the last created one.

    What exactly is not working?

    Cheers,
    _

  3. #3

    Default Re: Multiple QTextEdit instanciations and EventFilter

    If I have three text edits, the event filter only works for one of them.
    The eventfilter is there to tell my mouseMoveEvent when to start making the textEdit move. This only works on the first textEdit I right-click on. I can click on the others as many times as I want, they won't move.

  4. #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: Multiple QTextEdit instanciations and EventFilter

    Your event filter explicitly checks for one object.
    It gets the events for all objects it is installed on but just returns false for most of them.

    Cheers,
    _

  5. #5

    Default Re: Multiple QTextEdit instanciations and EventFilter

    Why does it return false event when I do the right-click (normally launching the eventfilter)? I don't understand...

  6. #6
    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: Multiple QTextEdit instanciations and EventFilter

    I am afraid I don't understand the question.

    The event filter is returning false unless it hits one of the two if conditions.

    Cheers,
    _

  7. #7

    Default Re: Multiple QTextEdit instanciations and EventFilter

    Never mind, thanks for your answers. I'll try and find the solution by myself. Too hard to explain on paper.

  8. #8

    Default Re: Multiple QTextEdit instanciations and EventFilter

    I've found something new about my problem today. With the code I gave, the event filter only works on the last textedit created (if I have 5 textEdits, it'll only work on the fifth one). As if there was an override somewhere.
    I don't know where that might come from, any ideas?

  9. #9
    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: Multiple QTextEdit instanciations and EventFilter

    Quote Originally Posted by Yaoming View Post
    I've found something new about my problem today. With the code I gave, the event filter only works on the last textedit created (if I have 5 textEdits, it'll only work on the fifth one).
    Fun fact: I said that in comment #2

    Quote Originally Posted by Yaoming View Post
    I don't know where that might come from, any ideas?
    Fun fact: I explained that in comment #2 and comment #4

    Cheers,
    _

  10. #10

    Default Re: Multiple QTextEdit instanciations and EventFilter

    Sorry but I don't know is how to correct it. I understand what you're saying but how to make the event filter check all the textEdits? I'm lost.

  11. #11

    Default Re: Multiple QTextEdit instanciations and EventFilter

    I read "If multiple event filters are installed on a single object, the filter that was installed last is activated first" in the documentation. Is there any way to change that?

  12. #12
    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: Multiple QTextEdit instanciations and EventFilter

    Quote Originally Posted by Yaoming View Post
    Sorry but I don't know is how to correct it. I understand what you're saying but how to make the event filter check all the textEdits? I'm lost.
    Your check compare the event receiver object "obj" with a pointer stored in a member variable called "txt".
    That variable can obviously only point to one of the text edits.

    Remove that part of the check.

    Cheers,
    __

  13. #13

    Default Re: Multiple QTextEdit instanciations and EventFilter

    Thanks, I didn't think about that^^
    Last edited by Yaoming; 16th February 2014 at 15:44.

Similar Threads

  1. Autorepeat within eventfilter
    By Patrick_Bao in forum Qt Programming
    Replies: 0
    Last Post: 1st December 2010, 07:37
  2. QTreeWidget and eventFilter
    By luoyes in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2009, 06:38
  3. sceneEventFilter or eventFilter
    By zgulser in forum Qt Programming
    Replies: 7
    Last Post: 4th May 2009, 07:50
  4. paintEvent in eventFilter
    By kernel_panic in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 20:59
  5. Multiple selection in QTextEdit
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2006, 07:58

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.