Mouse move event problems
-
Hello,
I am using QT 5.14.1 (in visual studio, but I tried the same on QT creator 4.11 ). I am new in QT and I am trying to do something really basic, which consists in a graphic view object where I want to know the mouse position.
My code is as follows:
mainwindow.h#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QGraphicsScene> #include <QTextStream> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); protected: void mouseMoveEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event); private: Ui::MainWindow *ui; QGraphicsScene* scene; }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { scene=new QGraphicsScene(parent); //I have tried with this aswell ui->setupUi(this); ui->graphicsView->setMouseTracking(true); ui->graphicsView->setScene(scene); QTextStream cout(stdout); cout << "Something that appears on the standard output"; } MainWindow::~MainWindow() { delete ui; } void MainWindow::mouseMoveEvent(QMouseEvent* event){ QTextStream cout(stdout); cout << "aa"; } void MainWindow::mousePressEvent(QMouseEvent* event){ QTextStream cout(stdout); cout << "oo"; }
My Ui only contains the graphicView
<?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>800</width> <height>600</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QGraphicsView" name="graphicsView"> <property name="geometry"> <rect> <x>180</x> <y>110</y> <width>256</width> <height>192</height> </rect> </property> </widget> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>800</width> <height>21</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui>
The problem is that I do not recieve "aaaaaaa" when I move the mouse over the graphicView (unless I press mouse outside and don't release mouse click). But I obtain "oo" each time I click with the mouse (everywhere not only in the GraphicView). What am I doing wrong?
Thanks in advanced,
-
@Siset
Don't you have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop (setMouseTracking(true)
) for theMainWindow
if you wantMainWindow::mouseMoveEvent()
to be called? You are only setting it for thegraphicsView
? (Or, if you want it there don't you have to doQGraphicsView::mouseMoveEvent()
?)See also perhaps https://forum.qt.io/topic/88459/qgraphicsview-mouse-move-event-not-work-to-me or https://stackoverflow.com/questions/21957314/mousemoveevent-in-qgraphicsview ?