Results 1 to 10 of 10

Thread: qmouseEvent handling

  1. #1
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default qmouseEvent handling

    hello everyone, i am trying to make a widget that will receive
    mousePressEvent(QMouseEvent *e) for a widget of mine.
    the thing is i have a QTextEdit that occupies most of the screen and
    thus mouseEvents do not propagate to the widget which is my receiver
    of choice for these events...
    i read about QMouseEvent::ignore() but i am not sure this is what i
    want... i basically want to make the textEdit (and any other objects
    like buttons ill add later) 'transparent' so mouseEvents are directly
    interpreted by the QWidget class.. ie the main window.
    thank you
    nass

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling


  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    im still abit fuzzy about its use..
    the thing is in QtDesigner i am not given the option to add code for another class (the QObject needed for handling events here)... so im not sure where to declare it since if i do that directly in .ui/myForm.h it will be deleted on the next make clean..

    but even besides that
    where am i supposed to run the installEventFilter() function? in init? in order to connect the mouseEvents received on the textEdit, to the mouseEvents handler of myForm...?
    nass

  4. #4
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    hm i started making some sense.. but it doesn't work properly.
    here is my .ui.h code:

    Qt Code:
    1. void alarmScreen::init()
    2. {
    3. textScreen->installEventFilter( this );
    4. }
    5.  
    6. void alarmScreen::closeButton_clicked()
    7. {
    8. //delete the alarm codes
    9. accept();
    10. }
    11.  
    12.  
    13. void alarmScreen::upButton_clicked()
    14. {
    15. textScreen->scrollBy(0,-60);
    16. }
    17.  
    18.  
    19. void alarmScreen::downButton_clicked()
    20. {
    21. textScreen->scrollBy(0,60);
    22. }
    23.  
    24.  
    25. void alarmScreen::mousePressEvent( QMouseEvent *e )
    26. {
    27. printf("%d,%d\n",e->x(),e->y());
    28. }
    29.  
    30.  
    31. bool alarmScreen::eventFilter( QObject *obj, QEvent *ev )
    32. {
    33. //printf("got in\n");
    34. if ( obj == textScreen )
    35. {
    36. printf("its the textEdit\n");
    37. if ( e->type() == QEvent::MouseButtonPress )
    38. {
    39. printf("ad%d,%d\n",e->x(),e->y());
    40. return TRUE;
    41. }
    42. }
    43. else
    44. {
    45. printf("af: %d,%d\n",e->x(),e->y());
    46. // pass the event on to the parent class
    47. //return QMainWindow::eventFilter( obj, ev );
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 

    basically my form (alarmScreen) is a QWidget (640x320) and on top of it sits a QFrame of the same dimensions then on the frame sit a QTextEdit (in read only mode) and 3 buttons (a closeBt, a scrollUp and a scrollDown). it basically a trivial log viewer but i did not use the standard scrollbars of the QTextEdit because i need bigger buttons.
    anyhow then i thought since it is so trivial i can delete the 2 scroll buttons and use the whole widget area for doing the scrolling (half the widget for scroll up and the other half for scroll down - forget the close button for a while).

    what i did manage with the above code .. is that when i 'cross' the border of the QTextEdit alarmScreen::eventFilter is run and the printf() statement is executed... but of course
    if ( e->type() == QEvent::MouseButtonPress fails since i have not clickd any button

    what i had initially done was to implement the
    void alarmScreen::mousePressEvent( QMouseEvent *e ) function
    but that only works when i click on the frame... not when i click somewhere insie the QTextEdit...

    any ideas or pointers?
    thank you for your help
    nass

  5. #5
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: qmouseEvent handling

    Quote Originally Posted by nass View Post
    what i did manage with the above code .. is that when i 'cross' the border of the QTextEdit alarmScreen::eventFilter is run and the printf() statement is executed... but of course
    if ( e->type() == QEvent::MouseButtonPress fails since i have not clickd any button
    So does it work when you click inside the textScreen? If so, then maybe you can just re-post this textScreen event to your widget object, so that your ::mousePressEvent will be called?

    Qt Code:
    1. QApplication::postEvent(alarmScreen, ev);
    To copy to clipboard, switch view to plain text mode 
    ?
    Software Engineer



  6. #6
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    nope it doesn't.
    it only works when the mouse pointer 'flies' over the edges of the textEdit box... as if it only works when it flies over the frame of the textEdit..

    but no the important thing is when i click on the textEdit the function eventFilter is not called...

    nass

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    try this:
    Qt Code:
    1. bool alarmScreen::eventFilter( QObject *obj, QEvent *ev )
    2. {
    3. //printf("got in\n");
    4. if ( obj == textScreen->viewPort() )
    5. {
    6. printf("its the textEdit\n");
    7. if ( e->type() == QEvent::MouseButtonPress )
    8. {
    9. printf("ad%d,%d\n",e->x(),e->y());
    10. return TRUE;
    11. }
    12. }
    13. else
    14. {
    15. printf("af: %d,%d\n",e->x(),e->y());
    16. // pass the event on to the parent class
    17. //return QMainWindow::eventFilter( obj, ev );
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jul 2006
    Posts
    79
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    i did that but now i can't even get to start the form... the eventFilter is run endlessly and sends the event directly to the parent:

    Qt Code:
    1. bool alarmScreen::eventFilter( QObject *obj, QEvent *ev )
    2. {
    3. //printf("got in\n");
    4. if ( obj == textScreen->viewport() )
    5. {
    6. printf("its the textEdit\n");
    7. if ( e->type() == QEvent::MouseButtonRelease )
    8. {
    9. printf("ad %d,%d\n",e->x(),e->y());
    10. return TRUE;
    11. }
    12. else
    13. {
    14. return FALSE;
    15. }
    16. }
    17. else
    18. if ( obj == this )
    19. {
    20. if ( e->type() == QEvent::MouseButtonRelease )
    21. {
    22. printf("af %d,%d\n",e->x(),e->y());
    23. return TRUE;
    24. }
    25. else
    26. {
    27. return FALSE;
    28. }
    29. }
    30. else
    31. {
    32. printf("sending event to parent");
    33. return alarmScreen::eventFilter( obj, ev );
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    it's very weird behaviour.. because when the mouse pointer flies over QTextEdits's frame border the eventFilter procedure is called and even when i click on the QTextEdits's frame border the right branch is called.. also when i click on the frame that lies (and covers) all of QWidget (alarmScreen) works properly..

    but, IN the white space of the QTextEdit (the writing space) it seems it is dead.. or that something else interecepts the mouse events before alarmScreen::eventFilter() can do so...
    thank you for your attempts to help
    nass
    Last edited by nass; 10th October 2006 at 11:42.

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    the eventFilter is run endlessly
    Thats is beacouse you made a recursive call:
    Qt Code:
    1. return alarmScreen::eventFilter( obj, ev );
    To copy to clipboard, switch view to plain text mode 
    There is no need for:
    Qt Code:
    1. else
    2. if ( obj == this )
    3. {
    4. if ( e->type() == QEvent::MouseButtonRelease )
    5. {
    6. printf("af %d,%d\n",e->x(),e->y());
    7. return TRUE;
    8. }
    9. else
    10. {
    11. return FALSE;
    12. }
    13. }
    14. else
    15. {
    16. printf("sending event to parent");
    17. return alarmScreen::eventFilter( obj, ev );
    18. }
    To copy to clipboard, switch view to plain text mode 
    Since you have methods to deal the objects own events.
    Use the event filter only to filter events of the observed object.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: qmouseEvent handling

    else
    {
    printf("sending event to parent");
    return alarmScreen::eventFilter( obj, ev );
    }
    I think what you meant here is to call the alarmScreen's parent class, in case it has reimplemented the eventFilter, if this is the case you should change
    return alarmScreen::eventFilter( obj, ev ); to
    return <alarmScreenParent>::eventFilter( obj, ev );

    Otherwise as I said, you have a recursive call here.

Similar Threads

  1. Keyboard Handling
    By ToddAtWSU in forum Qt Programming
    Replies: 4
    Last Post: 5th July 2006, 13:25
  2. Qt's Exception Handling
    By bruce1007 in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 09:30
  3. How to get color of pixel or point by QMouseEvent
    By Krishnacins in forum Newbie
    Replies: 4
    Last Post: 28th May 2006, 01:46
  4. string handling
    By vijay anandh in forum Qt Programming
    Replies: 4
    Last Post: 5th May 2006, 09:15
  5. QToolBox and QMouseEvent
    By mickey in forum Qt Programming
    Replies: 6
    Last Post: 1st February 2006, 11:57

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.