本文介绍了Qt mouseReleaseEvent()没有触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库来显示图片,可以调用它PictureGLWidget:



class PictureGLWidget:public QGLWidget {



因此PictureGLWidget扩展了QGLWidget。在PictureGlWidget中,

  void PictureGlWidget :: mouseReleaseEvent(QMouseEvent * releaseEvent); 



b

一个自己的项目,让我说类MyMainWindow,其中我只是使用PictureGlWidget作为Pointerobject:

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

这里,我已经可以看到PictureGlWidget和MainwindowWidget中的相应图片。当我点击那个PictureGlWidget时,按住鼠标,我可以移动图片(像2D滚动),因为它比我的小MainWindow大得多。



PictureGlWidget提供了一个函数

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

这只是告诉我图片中心的位置,我释放了图片的当前剪辑。 Remeber我的图片比我的小MainWindowWidget大得多,因此比我的PictureGLWidget更大。想象一下,图片有4000x4000px(0,0左上)。 PictureGLWidget只是显示让我们说800x800px。所以getPictureLocation()设置当前显示图片部分的中心cooridinates,它会返回类似(400,400),这可能在midldle左上角的某个地方。



我想抓住当前显示的图片部分(只是大图片的一小部分)中心位置,然后在该Widget中滚动并释放鼠标。我想通过覆盖



MyMainWindow :: mouseReleaseEvent(QMouseEvent * event){qDebug()< 鼠标释放! }



方法,但在任何地方都没有连接。

解决方案

QWidget中的虚拟保护方法,你可以覆盖对某些事件作出反应不需要连接。这些不是Qt槽,但经典功能Qt在必要时自动调用。



如,如果实现 PictureGlWidget :: mouseReleaseEvent(QMouseEvent *)接受事件,未传播到父窗口小部件。但是你可以为你的PictureGLWidget安装一个事件过滤器,并在发送之前接收事件。

  PictureGlWidget * myPictureGLWidget = new PictureGlWidget (...); 
layout-> addWidget(myPictureGLWidget,0,1);
myPictureGLWidget-> installEventFilter(this);然后在您的主窗口中实现正确的方法:









$ b b

  bool MyMainWindow :: eventFilter(QObject * object,QEvent * event)
{
if(object == myPictureGLWidget&& event-> type()== QEvent :: MouseButtonRelease){
QMouseEvent * mouseEvent = static_cast< QMouseEvent *>(event);
//在这里你需要做的事
}
//事件将被正确地发送到窗口小部件
return false;
//现在要停止事件传播:
// return true
}

你甚至可以决定,在做了你必须做的事情后,你想要停止事件,还是把它发送到PictureQLWidget instace(正常的行为)。



文件:







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.

解决方案

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:

这篇关于Qt mouseReleaseEvent()没有触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 14:43