Qt mouseReleaseEvent() not trigggered?

邮差的信 提交于 2019-12-11 02:07:09

问题


I got a library to display pictures, lets call it PictureGLWidget, with:

class PictureGLWidget: public QGLWidget {

so PictureGLWidget extends QGLWidget. In PictureGlWidget the

  void PictureGlWidget::mouseReleaseEvent(QMouseEvent* releaseEvent);

is already implemented.

I started an own project, lets say class MyMainWindow, where I just use a PictureGlWidget as a Pointerobject:

PictureGlWidget * myPictureGLWidget = new PictureGlWidget(...);
//..
layout->addWidget(myPictureGLWidget , 0, 1);

Here at this point, I already can see the PictureGlWidget and the corresponding picture in my MainwindowWidget. When I click in that PictureGlWidget, hold the mouse, I can move the picture (like 2D-scrolling), since it is much bigge than my little MainWindow.

Further on PictureGlWidget provides a function

bool PictureGlWidget::getPictureLocation(double& xPos, double& yPos);

which just tells me the Pictures center position, where I released the current clipping of the picture. Remeber my picture is much bigger than my little MainWindowWidget and thus much much more bigger than my PictureGLWidget. Imagine the picture has 4000x4000px (0,0 upper left). The PictureGLWidget is only to display lets say 800x800px. So the getPictureLocation() sets the center cooridinates of the current displayed picture part and it would return something like (400, 400), which might be somewhere in the midldle upper left corner.

I would like to grab the current displayed pictureparts (just a little part of that big picture) center position, after scrolling in that Widget and I released the mouse. I thought I do that by overwriting the

MyMainWindow::mouseReleaseEvent(QMouseEvent *event){ qDebug() << "Mouse released!"; }

method, but did not connected it anywhere yet. Currently it is not reacting on my mouseReleases and that text is not displayed.


回答1:


The virtual protected methods in QWidget that you can override to react on some events don't need to be "connected". These are not Qt slots but classical functions Qt automatically calls when necessary.

As explained in Qt Event system doc, if the implementation PictureGlWidget::mouseReleaseEvent(QMouseEvent*) accept the event, it is not propagated to the parent widget. But you can install an event filter to your PictureGLWidget and receive events before they are sent to it.

PictureGlWidget * myPictureGLWidget = new PictureGlWidget(...);
layout->addWidget(myPictureGLWidget , 0, 1);
myPictureGLWidget->installEventFilter(this);

Then implements the right method in your main window:

bool MyMainWindow::eventFilter(QObject *object, QEvent *event)
{
    if (object == myPictureGLWidget && event->type() == QEvent::MouseButtonRelease) {
        QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
        // Do what you need here
    }
    // The event will be correctly sent to the widget
    return false;
    // If you want to stop the event propagation now:
    // return true
}

You can even decide if, after doing what you have to do, you want to stop the event, or send it to the PictureQLWidget instace (the normal behavior).

Doc:

  • http://doc.qt.io/qt-4.8/qobject.html#installEventFilter
  • http://doc.qt.io/qt-4.8/qobject.html#eventFilter



回答2:


Do not forget the Q_OBJECT keyword in your MyGLwidget custom class declaration



来源:https://stackoverflow.com/questions/31517566/qt-mousereleaseevent-not-trigggered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!