如何识别QGraphicsView鼠标移动事件?

时间:2018-03-03 18:56:21

标签: c++ qt qgraphicsview qmouseevent

我是qt和c ++的新手。我有一个qgraphicsview来绘制信号。我会用鼠标点击和矩形绘图缩放特定区域。所以我需要鼠标按下位置和拖动位置。为此,我这样做:

  头文件中的

#include <QtWidgets/QMainWindow>
#include <QGraphicsScene>
#include <QMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include "ui_QtGuiApplication.h"

class QtGuiApplication : public QMainWindow
{
Q_OBJECT

public:
QtGuiApplication(QWidget *parent = Q_NULLPTR);

protected:
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
bool eventFilter(QObject *obj, QEvent *ev);

private:
Ui::QtGuiApplicationClass ui;
QPoint Zoom_point1_;
QPoint Zoom_point2_;
QGraphicsScene* scene = new QGraphicsScene();

};
  源文件中的

    QtGuiApplication::QtGuiApplication(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    ui.graphicsView->installEventFilter(this);
    ui.graphicsView->setMouseTracking(true);
}

bool QtGuiApplication::eventFilter(QObject * obj, QEvent * ev)
{
    if (obj == ui.graphicsView)
        if (ev->type() == QEvent::MouseMove)
        {
            QMouseEvent *mEvent = (QMouseEvent*)ev;
            Zoom_point2_ = mEvent->pos();
        }
    return false;
}
void QtGuiApplication::mouseMoveEvent(QMouseEvent * ev)
{
    Zoom_point2_ = ev->globalPos();
    //do some thing …
}

void QtGuiApplication::mousePressEvent(QMouseEvent * ev)
{
    Zoom_point1_ = ev->globalPos(); 
}

当我在图形视图中按下并移动鼠标时,我可以识别单击的位置,但mouseMoveEvent(QMouseEvent * ev)永远不会被调用。 obj == ui.graphicsView中的eventFilter声明也永远不会发生。我怎么了?我该如何解决?

1 个答案:

答案 0 :(得分:-1)

通过在QgraphicsView对象的视口上安装事件过滤器,例如

    import java.util.ArrayList;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;

    public class HandlingWindows {

    public static void main(String[] args) throws InterruptedException
    {

        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        String parent= driver.getWindowHandle();
        System.out.println("Parent Window is"+parent);

        //Get Data Policy
        WebElement we= driver.findElement(By.linkText("Data Policy"));
        //Click Data Policy link
        we.click();
        //Create an arrayList
        ArrayList<String> s1= new ArrayList<String>(driver.getWindowHandles());


         for(String s2:s1)
           {


            if(!(s2.equalsIgnoreCase(parent)))
              {
               driver.switchTo().window(s2);
               Thread.sleep(5000);
               System.out.println(driver.getWindowHandle());
               System.out.println("get title of window"+driver.getTitle());
              }
             }
    }

}

并在@media only screen and (max-width:1000px){ .main-content{width:100%} }

上添加以下条件
ui.graphicsView->viewport()->installEventFilter(this);

问题解决了。