Results 1 to 4 of 4

Thread: Create a QWheelEvent and pass it to a widget's wheelEvent

  1. #1
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Create a QWheelEvent and pass it to a widget's wheelEvent

    Hello.

    I am creating a grid (like a graph) from a QGraphicsView adding zooming capabilities.
    If i use the scrollwheel on the graphicsview, the application zoomes the view as intended.
    I now want to map a button to emit a QWheelEvent signal to the graphicsview widget.
    (a zoom button)

    the slot function for the buttons clicked() signal.
    Qt Code:
    1. // slot function for the clicked() signal of the zoombutton
    2. void DSATS::plusZoom()
    3. {
    4. std::cout << "should zoom" << std::endl;
    5.  
    6. QWheelEvent * event = new QWheelEvent(QPoint(10, 10), 120, Qt::LeftButton, Qt::NoModifier);
    7. QApplication::postEvent(dsatsGrid, event);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    Here dsatsGrid is a subclassed QGraphicsView.


    Qt Code:
    1. // wheelevent handler in the grid-class
    2. void GfxGrid::wheelEvent(QWheelEvent* event)
    3. {
    4.  
    5. std::cout << "wheelevent recieved" << std::endl;
    6.  
    7. ...
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    When i click the button, the function zoomPlus is called as expected, but the wheelevent never reaches its intended target.
    I have tried with both postEvent and sendEvent, but it makes no difference. (although i know postEvent should be used because its thread safe and handles the memory)

    Thank you for any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Create a QWheelEvent and pass it to a widget's wheelEvent

    Creating a event to post it seems to be a little bit too much in my eyes. Better create a public function for that and call it direct:
    Qt Code:
    1. void GfxGrid::zoom(qreal factor) //...
    2.  
    3. void GfxGrid::wheelEvent(/*...*/)
    4. {
    5. // calculate factor
    6. zoom(factor);
    7. }
    8.  
    9. void DSATS::plusZoom()
    10. {
    11. dsatsGrid->zoom(1.1);
    12. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Create a QWheelEvent and pass it to a widget's wheelEvent

    Hello, and thanks for the quick reply.

    I realize the problem can be solved as easy as you describe, but am curious to know why it cannot/does not work the way i tried it. As i understand it, the postEvent()-function and the corresponding event type is meant to accomplish exactly what i am trying to achieve.

    Thanks for your time.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Create a QWheelEvent and pass it to a widget's wheelEvent

    It should work, but probably the event gets eaten somewhere else. How do you have defined the pointer dsatsGrid? Can you make a small executable project reproducing your problem.

    EDIT: Here I made a simple working pice of code:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Test : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. Test(QWidget *parent) : QWidget(parent)
    8. {}
    9.  
    10. protected:
    11. void wheelEvent(QWheelEvent *event)
    12. {
    13. qWarning() << Q_FUNC_INFO << event;
    14. }
    15. };
    16.  
    17.  
    18. class Base : public QPushButton
    19. {
    20. Q_OBJECT
    21.  
    22. public:
    23. Base(QWidget *parent) : QPushButton(parent)
    24. {
    25. setText("send weel event");
    26. test = new Test(this);
    27. connect(this, SIGNAL(clicked()),this, SLOT(send()));
    28. }
    29.  
    30. private Q_SLOTS:
    31. void send()
    32. {
    33. QWheelEvent event(QPoint(10, 10), 120, Qt::LeftButton, Qt::NoModifier);
    34. QApplication::sendEvent(test, &event);
    35. }
    36.  
    37. private:
    38. Test *test;
    39. };
    40.  
    41.  
    42.  
    43.  
    44. int main(int argc, char *argv[])
    45. {
    46. QApplication app(argc, argv);
    47.  
    48. Base b(0);
    49. b.show();
    50.  
    51. return app.exec();
    52. }
    53.  
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 20th December 2010 at 15:08.

Similar Threads

  1. Pass event (QWheelEvent) to a specific widget
    By stefanadelbert in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2021, 04:10
  2. How to disable wheelEvent()
    By Markus in forum Qt Programming
    Replies: 3
    Last Post: 6th August 2010, 19:42
  3. Pass mouseEvent to sibling widget?
    By nish in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2009, 14:00
  4. Replies: 1
    Last Post: 26th July 2009, 16:08
  5. pass mouse event information to another widget
    By Rooster in forum Qt Programming
    Replies: 5
    Last Post: 12th July 2008, 05:23

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.