saracaeus
8th September 2011, 09:25
hello,
I want to send mouse events to my class derived from QMainWindow. Therefore I installed an event filter which stores the corresponding events and then, if completed, sends to the main window. Basicly it is a mouse press, mouse move and mouse release event. I can see the events sent by sendEvent() in the eventFilter, but seem not to work within the main window (no reaction).
Here is the code:
bool MyMainWindow::eventFilter( QObject *pObj, QEvent *pEvent )
{
static bool bInternalSend = false;
static QMouseEvent cStartEvent(QEvent::MouseButtonPress, QCursor::pos(), Qt::MouseButton::LeftButton, 0, 0 );
static QMouseEvent cLastMoveEvent(QEvent::MouseMove, QCursor::pos(), Qt::MouseButton::LeftButton, 0, 0 );
if ( !bInternalSend )
{
if ( pEvent->type() == QEvent::MouseButtonPress )
{
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
cStartEvent = *pMouseEvent;
this->StartRubberBand(); // show the rubber band at the current mouse position, depending on the mouse cursor shape
return true;
}
else if ( pEvent->type() == QEvent::MouseButtonRelease )
{
this->EndRubberBand(); // hide the rubber band
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
{
bInternalSend = true;
QApplication::sendEvent( this, &cStartEvent ); // send mouse press event
bInternalSend = true;
QApplication::sendEvent( this, &cLastMoveEvent ); // send last mouse move event
}
}
else if ( pEvent->type() == QEvent::MouseMove )
{
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
cLastMoveEvent = *pMouseEvent;
this->MoveRubberBand(); // move the rubber band to the new mouse position
return true;
}
}
bInternalSend = false;
return QMainWindow::eventFilter( pObj, pEvent );
}
Is it in general not possible to send mouse events to the main window? Or what is my fault?
Any help is appreciated!
I want to send mouse events to my class derived from QMainWindow. Therefore I installed an event filter which stores the corresponding events and then, if completed, sends to the main window. Basicly it is a mouse press, mouse move and mouse release event. I can see the events sent by sendEvent() in the eventFilter, but seem not to work within the main window (no reaction).
Here is the code:
bool MyMainWindow::eventFilter( QObject *pObj, QEvent *pEvent )
{
static bool bInternalSend = false;
static QMouseEvent cStartEvent(QEvent::MouseButtonPress, QCursor::pos(), Qt::MouseButton::LeftButton, 0, 0 );
static QMouseEvent cLastMoveEvent(QEvent::MouseMove, QCursor::pos(), Qt::MouseButton::LeftButton, 0, 0 );
if ( !bInternalSend )
{
if ( pEvent->type() == QEvent::MouseButtonPress )
{
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
cStartEvent = *pMouseEvent;
this->StartRubberBand(); // show the rubber band at the current mouse position, depending on the mouse cursor shape
return true;
}
else if ( pEvent->type() == QEvent::MouseButtonRelease )
{
this->EndRubberBand(); // hide the rubber band
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
{
bInternalSend = true;
QApplication::sendEvent( this, &cStartEvent ); // send mouse press event
bInternalSend = true;
QApplication::sendEvent( this, &cLastMoveEvent ); // send last mouse move event
}
}
else if ( pEvent->type() == QEvent::MouseMove )
{
QMouseEvent *pMouseEvent = dynamic_cast<QMouseEvent*>(pEvent);
if ( pMouseEvent )
cLastMoveEvent = *pMouseEvent;
this->MoveRubberBand(); // move the rubber band to the new mouse position
return true;
}
}
bInternalSend = false;
return QMainWindow::eventFilter( pObj, pEvent );
}
Is it in general not possible to send mouse events to the main window? Or what is my fault?
Any help is appreciated!