Reputation: 255
In a recent project, I tried to draw points on a scene after clicking on the corresponding location within GraphicsView, but nothing happend. I tracked the error down to a missing call of ClickableMap::mousePressEvent(const QMouseEvent&)
.
In this minimal example, nothing is printed after a mouse click on the white Widget, but a message should be printed. I will load an image into the scene, if needed.
What goes wrong here? Many thanks in advance!
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();
}
mainwindow.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;
}
mainwindow.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
Upvotes: 2
Views: 1411
Reputation: 243887
When overriding parent methods it is necessary to copy the names exactly as they were written to the parent, according to the docs:
void QGraphicsView::mousePressEvent(QMouseEvent *event)
That is to say it receives as a parameter a pointer and not a reference, in your case you must make the following changes:
*.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);
}
You should also change the following:
connect(ui->graphicsView, &ClickableMap::mousePressed, this, erster_klick);
to:
connect(ui->graphicsView, &ClickableMap::mousePressed, this, &MainWindow::erster_klick);
Upvotes: 2