c++ - QGraphicsView的MousePressEvent没有被调用

标签 c++ qt qt5 qgraphicsview

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

在这个最小的示例中,鼠标单击白色小部件后不会打印任何内容,但应打印一条消息。如果需要,我会将图像加载到场景中。

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

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

最佳答案

根据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);

关于c++ - QGraphicsView的MousePressEvent没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46286090/

相关文章:

python - PyQt5 失败并显示神秘消息

c++ - 缓解 "type-validate then use"代码重复模式

qt - 如何通过 Qt 项目文件将命令的输出作为编译器标志传递?

Qt - 什么是示例缓冲区?

qt - 如何部署使用ODBC插件的QT应用程序(无静态链接)?

c++ - 更改对话框的父级禁用拖放

qt - 与 OpacityMask 相反

c++ - 如何使用 Winsock API 控制连接超时?

c# - WM_SIZING 上的 GetClientRect 给出了错误的大小

c++ - 如何将文件内容识别为 ASCII 或二进制