why postevent cause crash
platform: linux, QT4.6.3
Hi All,
I want to simulate the mouse left click event
here is code snippets.
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
view_->setScene(scene_);
}
MainWindow::~MainWindow()
{
delete ui;
}
{
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
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.
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:
Code:
void MainWindow::on_pushButton_clicked()
{
Qt::LeftButton,
Qt::LeftButton,
Qt::NoModifier
)
);
}
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
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
Code:
{
public:
protected:
void mousePressEvent
(QMouseEvent *) //reimplement mousePressEvent {
qDebug()<<"myview";
}
};
//.....
void MainWindow::on_pushButton_clicked()
{
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
}
{
if(event->button() == Qt::RightButton)
{
qDebug()<<"right";
}
else
{
qApp->postEvent(view_,event); // will excute when click button
}
}
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.
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
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.