Results 1 to 8 of 8

Thread: why postevent cause crash

  1. #1
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Question why postevent cause crash

    platform: linux, QT4.6.3

    Hi All,

    I want to simulate the mouse left click event
    here is code snippets.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QMouseEvent>
    4. #include <QGraphicsScene>
    5. #include <QGraphicsView>
    6. #include <QDebug>
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. scene_ = new QGraphicsScene(0,0, 480, 800,this);
    14. view_ = new QGraphicsView(this);
    15. view_->setScene(scene_);
    16.  
    17.  
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::mousePressEvent(QMouseEvent *event)
    26. {
    27. if(event->button() == Qt::RightButton)
    28. {
    29. qDebug()<<"right";
    30. }
    31. else
    32. {
    33. qApp->postEvent(view_,event); //will crash here
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    when click left button on window, I want forward it to QGraphicsScene (I don't want to use the event filter). but when I click the left button,the app crashed,I was wondering why

    Thanks advance for your help.
    Best regards,
    hb

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

    Default Re: why postevent cause crash

    The event should reach the child widget before hitting the parent so if you are getting the event in the window then most likely it means you didn't handle it in the view. As for the crash, you are posting an event that will soon get deleted by Qt after leaving the event handler. Either send the event to the other widget or create a new event and post that.
    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.


  3. The following user says thank you to wysota for this useful post:

    hashb (31st August 2010)

  4. #3
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: why postevent cause crash

    Hi wysota,

    Thanks a lot for your reply.
    I create a QMouseEvent ,but it also crashed,any ideas about it?
    here is the code:
    Qt Code:
    1. void MainWindow::on_pushButton_clicked()
    2. {
    3. qApp->postEvent(view_,new QMouseEvent(
    4. (QEvent::MouseButtonPress),
    5. QPoint(100,100),
    6. Qt::LeftButton,
    7. Qt::LeftButton,
    8. Qt::NoModifier
    9. )
    10. );
    11. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: why postevent cause crash

    Hi ,

    I have found out the reason ,
    it was because view_ didn't re-implement the mousepressevent ,so when executing
    qApp->postEvent(view_,event);
    view_ will send the event to its parent directly which will cause a dead loop in MainWindow::mousePressEvent

    Best regards,
    hb

  6. #5
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: why postevent cause crash

    another problem:

    I found that even I have re-implement the mousePressEvent of view_, it cann't prevent the event forward to its parent


    Qt Code:
    1. class myview:public QGraphicsView
    2. {
    3. public:
    4. myview(QWidget *parent):
    5. QGraphicsView(parent){}
    6. protected:
    7. void mousePressEvent(QMouseEvent *) //reimplement mousePressEvent
    8. {
    9. qDebug()<<"myview";
    10. }
    11. };
    12.  
    13. //.....
    14.  
    15. void MainWindow::on_pushButton_clicked()
    16. {
    17. qApp->postEvent(view_,new QMouseEvent(
    18. (QEvent::MouseButtonPress),
    19. QPoint(100,100),
    20. Qt::LeftButton,
    21. Qt::LeftButton,
    22. Qt::NoModifier
    23. )
    24. );
    25. //when click button,I hope the myview::mousePressEvent will be excuted,
    26. //but in fact MainWindow::mousePressEvent excuted which cause a dead loop.
    27. //so I guess QT can not translate QMouseEvent to mousepressevent automatically
    28. }
    29.  
    30. void MainWindow::mousePressEvent(QMouseEvent *event)
    31. {
    32. if(event->button() == Qt::RightButton)
    33. {
    34. qDebug()<<"right";
    35. }
    36. else
    37. {
    38. qApp->postEvent(view_,event); // will excute when click button
    39. }
    40. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why postevent cause crash

    Somehow I'm not surprised as you shouldn't need to be forwarding anything. What exactly are you trying to do? Why not handle the mouse in the scene? In general forwarding every mouse event to the view is probably not a good idea.
    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.


  8. #7
    Join Date
    Jun 2009
    Posts
    74
    Thanks
    23
    Thanked 2 Times in 2 Posts

    Default Re: why postevent cause crash

    Hi wystoa,
    why forward mouse click :
    the following link is my question
    http://www.qtcentre.org/threads/3377...click-behavior

    Best regards,
    hb

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: why postevent cause crash

    The thread doesn't say why you want to forward a click. Ignored GUI events are forwarded to the parent out of the box so you shouldn't need any "forwarding", especially in the reverse direction.
    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.


Similar Threads

  1. postEvent and multithreading
    By Thymson in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2008, 14:22
  2. QCoreApplication::postEvent();
    By chrisdsimpson in forum Newbie
    Replies: 6
    Last Post: 1st April 2008, 22:23
  3. postEvent & Thread problems
    By gianpatt in forum Qt Programming
    Replies: 4
    Last Post: 21st March 2008, 15:19
  4. postEvent thread problem
    By jhamers in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2008, 11:36
  5. PostEvent
    By user_mail07 in forum Qt Programming
    Replies: 8
    Last Post: 1st June 2007, 01:01

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.