2017-09-18 13 views
0

最近のプロジェクトでは、GraphicsView内の対応する場所をクリックした後、シーンにポイントを描画しようとしましたが、何も起こりません。私はClickableMap::mousePressEvent(const QMouseEvent&)の不在着信にエラーを追跡しました。QGraphicsViewのMousePressEventが呼び出されていない

この最小の例では、白いウィジェットをマウスでクリックしても何も印刷されませんが、メッセージは印刷されます。必要に応じて、シーンに画像をロードします。

ここで何が問題になるのですか?事前に多くの感謝!

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 

答えて

0

親のメソッドをオーバーライドするとき、彼らがいたとおりに名前をコピーする必要があります親に書かれている、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); 
+0

は、関数のシグネチャを比較する厳格な方法以外にも、私のバージョンをallwowingないためのいずれかの理由がありますか? &MainWindow ::プリフィックスはMainWindowコンストラクタ自体にも必要ですか?もしそうなら、なぜですか? 'connect(ボタン、&QPushButton :: clicked、this、verschiebe);'コンストラクタ内の別のコードで完全に動作します。 ( 'void MainWindow :: verschiebe(QPoint)') – user7427029

+0

私はあなたの接続フォームを使用すると、次のエラーが表示されます:**/home/qhipa/test/mainwindow。cpp:12:error:非staticメンバ関数の無効な使用 'void MainWindow :: erster_klick(const QPoint&)' connect(ui-> graphicsView、&ClickableMap :: mousePressed、this、erster_klick); ** – eyllanesc

+0

@ user7427029答えは私の答えを正しいものとしてマークしてください。 – eyllanesc

関連する問題