It's true that MousePressEvent is a function, but I like to think of it differently than ordinary functions that you just call directly. It's an event handler, meant to handle event objects, and it's essentially a single component of a system of objects working together.
I believe you can still do what I think you're trying to do by constructing a new QMouseEvent object first. The following code snippet "fakes" a mouse press when the user presses the spacebar:
void MyWidget
::keyPressEvent( QKeyEvent* event
) {
if( event->key() == Qt::Key_Space )
{
qDebug() <<"SPACE - "<<m_pointMouseHover;
QMouseEvent* newMouseEvent
= new QMouseEvent( QEvent::MouseButtonPress, m_pointMouseHover, Qt
::LeftButton, Qt
::LeftButton, Qt
::NoModifier );
mousePressEvent( newMouseEvent );
delete newMouseEvent;
event->accept();
return;
}
event->ignore();
}
void MyWidget::keyPressEvent( QKeyEvent* event )
{
if( event->key() == Qt::Key_Space )
{
qDebug() <<"SPACE - "<<m_pointMouseHover;
QMouseEvent* newMouseEvent = new QMouseEvent( QEvent::MouseButtonPress, m_pointMouseHover, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier );
mousePressEvent( newMouseEvent );
delete newMouseEvent;
event->accept();
return;
}
event->ignore();
}
To copy to clipboard, switch view to plain text mode
Even though this is more complex than simple function calls as you're starting, I've found that this approach makes it far easier to do the sort of things more complicated widgets end up needing to do.
Hope this helps!
Bookmarks