Results 1 to 7 of 7

Thread: How to pass events from QWidget covering QMainWindow?

  1. #1
    Join Date
    Nov 2011
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 1 Time in 1 Post

    Default How to pass events from QWidget covering QMainWindow?

    I have QGraphicsView that covers main window (its size and QGraphicsScene size are the same as the window).
    I'm useing it to display animated hints.
    I made it transparent for mouse events by
    Qt Code:
    1. setAttribute(Qt::WA_TransparentForMouseEvents, false);
    To copy to clipboard, switch view to plain text mode 

    I would like to make those hints clikable so I need to change thet attribute,
    but then main window is covered from mouse events.
    I tried to reimplement QGraphicsView::event
    Qt Code:
    1. bool MyGraphicsView::event(QEvent* event) {
    2. m_parent->event(event);
    3. return QGraphicsView::event(event);
    4. }
    To copy to clipboard, switch view to plain text mode 
    m_parent is QMainWindow
    Qt Code:
    1. bool MainWindow::event(QEvent *event) {
    2. return QMainWindow::event(event);
    3. }
    To copy to clipboard, switch view to plain text mode 

    but it crash on:
    Qt Code:
    1. return QMainWindow::event(event);
    To copy to clipboard, switch view to plain text mode 

    Yes, I can adjust QGraphicsView to some hints and cover only needed part of the window
    but it is much of work.
    Maybe Is there any trick?

  2. #2
    Join Date
    Dec 2012
    Posts
    90
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 20 Times in 18 Posts

    Default Re: How to pass events from QWidget covering QMainWindow?

    QWidget::event () function is protected for a reason, you shouldn't make it public.
    You experiencing crash because you are propagating ALL the events (and it's ChildAdded event that messes up internal mechanics and crashes the app).

    To achieve desired functionality you should reimplement only mouseEvent () and use post/sendEvent to propagate mouse events to the MainWindow.

  3. #3
    Join Date
    Nov 2011
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 1 Time in 1 Post

    Default Re: How to pass events from QWidget covering QMainWindow?

    Thans for reply.

    It doesn't work.
    I debuged MainWindow::event(), it gots MouseButtonPress event there but it isn't propagated to appropirate Buttons over those was clicked.

    Also i tried opposite:
    I made QGraphicsView transparent for mose events and I sended MainWindow events to it.
    Also QGraphicsView::event() handler sees the events but QGraphicsTextItem doesn't send linkActivated(QString) signal after click.
    In this case (MainWindow sends events to QGraphicsView)
    all what I need it to get this signal working.
    I have
    setTextInteractionFlags(Qt::TextBrowserInteraction );
    and this works when
    setAttribute(Qt::WA_TransparentForMouseEvents) is set to false

    So what do You think about it?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: How to pass events from QWidget covering QMainWindow?

    Don't make anything transparent to anything. Your problem is that you are trying to handle events in an incorrect place. For handling events that happen in graphics view (e.g. for graphics item), handle them in Graphics View architecture (i.e. in the scene. the view or in items themselves). If you want to handle events that happen outside the graphics view, handle them in a widget containing the view or in the view itself. If an event is ignored, it will propagate to the parent thus if you have a graphics view over some other widget and you makre sure the event is ignored, it will pass through to the parent widget. See QEvent::ignore().

    Another thing is whether doing animated hints by covering your main widget with graphics view is that good of an idea. It seems much simpler to make a hint a custom widget, possibly one that makes use of Qt::ToolTip.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2011
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 1 Time in 1 Post

    Default Re: How to pass events from QWidget covering QMainWindow?

    I reimplemented QGraphicsScenemethods:
    mousePressEvent(QGraphicsSceneMouseEvent* event) and
    mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
    I had got for each method:
    Qt Code:
    1. myScene::mousePressEvent(QGraphicsSceneMouseEvent* event) {
    2. event->ignore();
    3. QGraphicsScene::mousePressEvent(event);
    4. }
    To copy to clipboard, switch view to plain text mode 
    then events started occur in QGraphicsView(That one wich covering the MainWindow)
    I did the same in QGraphicsView reimplementation
    but events heven't occured in MainWindow
    When I call
    Qt Code:
    1. myQGraphicsView::mousePressEvent(QMouseEvent *event) {
    2. event->ignore();
    3. qApp->sendEvent(parent, event);
    4. }
    To copy to clipboard, switch view to plain text mode 

    MainWindow invokes mousePressEvent()
    but widgets in MainWindow can't get it even i put event->ignore() inside

    Any ideas?

    P.S.
    I found in the web a few similar threads but none solved

  6. #6
    Join Date
    Nov 2011
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 1 Time in 1 Post

    Default Re: How to pass events from QWidget covering QMainWindow?

    I found a reason (I think) why MainWindow doesn't propagate that events.
    It seems, events are moving "up to the hill" from children to parent
    QGraphicsScene->QGraphicsView->MainWindow
    but MainWindow can't send that event to other children back.

    Also I tried opposite
    I'm handling mousePress and mouseRelease events in
    QMainWindow::event() reimplementation
    Qt Code:
    1. if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
    2. myQGraphicsView->event(event);
    3. }
    To copy to clipboard, switch view to plain text mode 
    in QGraphicsView the events are nicely propageted to its scene
    and reimpemented method
    QGraphicsTextItem::event()shows those events
    But i don't know why QGraphicsTextItem doesn't emit linkActiveted signal
    (I'm sure all is redy for this, I mean
    item->setTextInteractionFlags(Qt::TextBrowserInteractio n);
    )

  7. #7
    Join Date
    Nov 2011
    Location
    Poland
    Posts
    17
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanked 1 Time in 1 Post

    Default [SOLVED] How to pass events from QWidget covering QMainWindow?

    I made this working.
    Let's assembly how.

    I have MainWindow (QMainWindow covered by QGraphicsView
    MainWindow have many widgets and View is used to show some tips and descriptions, sometimes animated.
    To keep MainWindow responsible on mouse events I must add
    Qt Code:
    1. myGraphicsView->setAttribute(Qt::WA_TransparentForMouseEvents)
    To copy to clipboard, switch view to plain text mode 

    but I needed QGraphicsTextItem placed in the View to be clickable and able to sending linkActivated(QString linkAddress)
    I obtained this by:
    1. Handling events in MainWindow and redirect some to myGraphicsView
    (QGraphicsView::event() is protected so classes have to be friends or event() reimplementation has to be public)
    Qt Code:
    1. bool MainWindow::event(QEvent *event) {
    2. if (event->type() == QEvent::MousePressEvent || event->type() == QEvent::MouseReleaseEvent) {
    3. myGraphicsView->event(event);
    4. }
    5. return QMainWindow::event(event);
    6. }
    To copy to clipboard, switch view to plain text mode 

    2. Invokeing mouse related methods in QGraphicsView::event() reimplementation

    Qt Code:
    1. myGraphicsView::event(QEvent *event) {
    2. if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease) {
    3. QMouseEvent *me = static_cast<QMouseEvent *>(event);
    4. me->setAccepted(false);
    5. if (event->type() == QEvent::MouseButtonPress)
    6. QGraphicsView::mousePressEvent(me);
    7. else
    8. QGraphicsView::mouseReleaseEvent(me);
    9. }
    10. return QGraphicsView::event(event);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Event are redirected to QGraphicsScene there and all is working as expected.
    Reimplementation of QGraphicsTextItem nicely emits linkActivated signal.

    For me it was enought to redirect exacly that two events but for some others porposes You maybe need more.

    Thanks for all clues.

Similar Threads

  1. Not possible to send mouse events to QMainWindow??
    By saracaeus in forum Qt Programming
    Replies: 8
    Last Post: 20th September 2011, 14:01
  2. Replies: 1
    Last Post: 29th May 2011, 09:00
  3. Replies: 3
    Last Post: 9th February 2011, 08:28
  4. Replies: 2
    Last Post: 4th November 2010, 11:10
  5. Pass custom events to another process
    By TTGator in forum Qt Programming
    Replies: 9
    Last Post: 14th January 2009, 23:02

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
  •  
Qt is a trademark of The Qt Company.