未调用 QGraphicsView 的 MousePressEvent

Posted

技术标签:

【中文标题】未调用 QGraphicsView 的 MousePressEvent【英文标题】:QGraphicsView's MousePressEvent is not called 【发布时间】:2017-09-18 18:38:50 【问题描述】:

在最近的一个项目中,我尝试在GraphicsView中单击相应位置后在场景上绘制点,但没有任何反应。我将错误追踪到缺少ClickableMap::mousePressEvent(const QMouseEvent&) 的调用。

在这个最小的例子中,鼠标点击白色 Widget 后没有打印任何内容,但应该打印一条消息。如果需要,我会将图像加载到场景中。

这里出了什么问题?非常感谢!

ClickableMap.h:

#ifndef CLICKABLEMAP_H
#define CLICKABLEMAP_H

#include <QMouseEvent>
#include <QGraphicsView>
#include <QPoint>

class ClickableMap: public QGraphicsView 
    Q_OBJECT

public:
    using QGraphicsView::QGraphicsView;  // seit C++ 11, benötigt wird mindestens GCC 4.8
    // Destruktor entfällt, da argumentlos

    void mousePressEvent(const QMouseEvent& event);

signals:
    void mousePressed(const QPoint&);
;

#endif // CLICKABLEMAP_H

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>

namespace Ui 
class MainWindow;


class MainWindow : public QMainWindow

    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

public slots:
    void erster_klick(const QPoint& punkt);
;

#endif // MAINWINDOW_H

ClickableMap.cpp:

#include <QMouseEvent>
#include <QPoint>
#include "ClickableMap.h"
#include <QDebug>
#include <QMessageBox>


void ClickableMap::mousePressEvent(const QMouseEvent& event)
    const QPoint& punkt = event.pos();
    qDebug() << "Maus gepresst";
    QMessageBox::information(this, tr("Dialog"), "Detected click in Drawspace");
    emit mousePressed(punkt);

main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();

主窗口.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)

    ui->setupUi(this);
    connect(ui->graphicsView, &ClickableMap::mousePressed, this, erster_klick);


MainWindow::~MainWindow()

    delete ui;


void MainWindow::erster_klick(const QPoint& punkt)
    qDebug() << "Ich bin nun im Slot";
    qDebug() << punkt;

主窗口.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="ClickableMap" name="graphicsView">
    <property name="geometry">
     <rect>
      <x>80</x>
      <y>20</y>
      <width>256</width>
      <height>192</height>
     </rect>
    </property>
    <property name="mouseTracking">
     <bool>true</bool>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>400</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <customwidgets>
  <customwidget>
   <class>ClickableMap</class>
   <extends>QGraphicsView</extends>
   <header>clickablemap.h</header>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>

Testanwendung.pro:

#-------------------------------------------------
#
# Project created by QtCreator 2017-09-18T19:53:12
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Testanwendung
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp \
    clickablemap.cpp

HEADERS += \
        mainwindow.h \
    clickablemap.h

FORMS += \
        mainwindow.ui

【问题讨论】:

【参考方案1】:

根据docs:

,当覆盖父方法时,必须完全按照写入父方法的方式复制名称
void QGraphicsView::mousePressEvent(QMouseEvent *event)

也就是说,它接收一个指针而不是一个引用作为参数,在你的情况下,你必须进行以下更改:

*.h

void mousePressEvent(QMouseEvent *event);

*.cpp

void ClickableMap::mousePressEvent(QMouseEvent *event)
    const QPoint& punkt = event->pos();
    qDebug() << "Maus gepresst";
    QMessageBox::information(this, tr("Dialog"), "Detected click in Drawspace");
    emit mousePressed(punkt);

您还应该更改以下内容:

connect(ui->graphicsView, &ClickableMap::mousePressed, this, erster_klick);

到:

connect(ui->graphicsView, &ClickableMap::mousePressed, this, &MainWindow::erster_klick);

【讨论】:

除了比较函数签名的严格方法之外,还有什么理由不让我的版本赞叹?即使在 MainWindow 构造函数本身中也需要 &MainWindow:: 前缀 - 如果是,为什么? 'connect(button, &QPushButton::clicked, this, verschiebe);'在构造函数的另一段代码中完美运行。 (使用 'void MainWindow::verschiebe(QPoint)') 我很惊讶它的工作原理,当我使用您的连接表单时,我收到以下错误:/home/qhipa/test/mainwindow.cpp:12:错误:无效使用非静态成员函数 'void MainWindow::erster_klick(const QPoint&)' connect(ui->graphicsView, &ClickableMap::mousePressed, this, erster_klick); @user7427029 如果我的回答对您有帮助,请将我的回答标记为正确。

以上是关于未调用 QGraphicsView 的 MousePressEvent的主要内容,如果未能解决你的问题,请参考以下文章

QGraphicsView 滚动条策略未按预期工作

未调用 QGraphicsItem 绘制

QGraphicsView:如何高效获取QGraphicsItems的视口坐标?

QGraphicsView Scale & QGraphicsRectItem 绘画未能调用

QGraphicsView 啥都不显示

动画 QGraphicsView fitInView 方法