PDA

View Full Version : why postevent cause crash



hashb
30th August 2010, 12:11
platform: linux, QT4.6.3

Hi All,

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



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene_ = new QGraphicsScene(0,0, 480, 800,this);
view_ = new QGraphicsView(this);
view_->setScene(scene_);


}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
qDebug()<<"right";
}
else
{
qApp->postEvent(view_,event); //will crash here
}
}


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

wysota
30th August 2010, 16:02
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.

hashb
31st August 2010, 03:03
Hi wysota,

Thanks a lot for your reply.
I create a QMouseEvent ,but it also crashed,any ideas about it?
here is the code:


void MainWindow::on_pushButton_clicked()
{
qApp->postEvent(view_,new QMouseEvent(
(QEvent::MouseButtonPress),
QPoint(100,100),
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier
)
);
}

hashb
31st August 2010, 03:29
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

hashb
31st August 2010, 04:36
another problem:

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




class myview:public QGraphicsView
{
public:
myview(QWidget *parent):
QGraphicsView(parent){}
protected:
void mousePressEvent(QMouseEvent *) //reimplement mousePressEvent
{
qDebug()<<"myview";
}
};

//.....

void MainWindow::on_pushButton_clicked()
{
qApp->postEvent(view_,new QMouseEvent(
(QEvent::MouseButtonPress),
QPoint(100,100),
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier
)
);
//when click button,I hope the myview::mousePressEvent will be excuted,
//but in fact MainWindow::mousePressEvent excuted which cause a dead loop.
//so I guess QT can not translate QMouseEvent to mousepressevent automatically
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::RightButton)
{
qDebug()<<"right";
}
else
{
qApp->postEvent(view_,event); // will excute when click button
}
}

wysota
31st August 2010, 08:59
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.

hashb
31st August 2010, 09:20
Hi wystoa,
why forward mouse click :
the following link is my question
http://www.qtcentre.org/threads/33772-how-to-simulate-mouse-click-behavior

Best regards,
hb

wysota
31st August 2010, 15:06
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.