PDA

View Full Version : Not possible to send mouse events to QMainWindow??



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!

wysota
8th September 2011, 11:02
What reaction would you expect exactly from the window? Based on the fact that QMainWindow probably doesn't reimplement any mouse events, if you expect some fireworks and music then it likely will not happen unless you code it by reimplementing mouse events from the main window object.

saracaeus
8th September 2011, 17:01
well, background is the following:

the Splitter within the QMainWindow is not implemented as a QSplitter, so I cannot switch off the opaque mode for handling when moving the splitter. The problem is, that when the user moves the splitter, all docked windows are updated at once and that causes a "chewing gum" effect. So I need to prevent the mouse events to be handled when the user moves the splitter (see the return true; in the eventFilter => event is handled and doesnt need to be handled by the parent any more). But when the user releases the mouse I have to send the mouse press event, ONE mouse move event and the mouse release event. so the splitter should be adjusted by the QMainWindow (at least that was my hope :)) But it is not.

Did I explain correctly? Or are there any more questions?

wysota
8th September 2011, 20:09
I don't know what a "chewing gum" effect is. Anyway instead of faking events maybe you should just find a method that will resize the dock widget.

saracaeus
8th September 2011, 21:19
I call it "chewing gum" effect when the splitter lags behind the mouse movement, because several docking windows are busy to repaint themselves. And as for each single pixel all the docking windows are repainted, this slows down the whole procedure. I would like to switch this opaque moving mode off, but as I initially mentioned it is not possible for the QMainWindow splitter. Thus I thought I simulate the whole mouse press and dragging process as one single step.

First I tried to somehow overwrite the resize event of the docking windows, but that's not working, somehow, then I tried to use the setUpdatesEnabled() function while moving, but that's not working for my case, because it seems that setUpdatesEnabled() is not thread-safe.

So then I thought I solve it with resending the mouse events as one step.

Nothing is working :(

wysota
8th September 2011, 22:33
Maybe you should focus on optimizing your dock widgets then :)

saracaeus
9th September 2011, 09:54
nope, because even if the dock widgets are empty, this lagging effect is there, because the QMainWindow mostly contains more than 5 dock widgets (and on this widget architecture I dont have influence on)

I need to find a way to emulate the non-opaque moving mode.

And I still hope anybody knows how I can resend these mouse events.

amleto
9th September 2011, 19:53
why not just use setOpaqueResize(false); on your splitter????

saracaeus
20th September 2011, 13:01
because the splitter in a QMainWindow is not derived from QSplitter... => there is no such function, I need to fix it manually