Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Mouse move event problems
Forum Updated to NodeBB v4.3 + New Features

Mouse move event problems

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Siset
    wrote on last edited by
    #1

    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,

    O JonBJ 2 Replies Last reply
    0
    • S Siset

      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,

      O Offline
      O Offline
      ofmrew
      wrote on last edited by
      #2

      @Siset The mouse move event is only triggered when the mouse button is pressed or mouse tracking has been set.

      1 Reply Last reply
      1
      • S Siset

        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,

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @Siset
        Don't you have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop (setMouseTracking(true)) for the MainWindow if you want MainWindow::mouseMoveEvent() to be called? You are only setting it for the graphicsView? (Or, if you want it there don't you have to do QGraphicsView::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 ?

        S 1 Reply Last reply
        2
        • JonBJ JonB

          @Siset
          Don't you have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop (setMouseTracking(true)) for the MainWindow if you want MainWindow::mouseMoveEvent() to be called? You are only setting it for the graphicsView? (Or, if you want it there don't you have to do QGraphicsView::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 ?

          S Offline
          S Offline
          Siset
          wrote on last edited by
          #4

          @JonB I was confused on the usage of QGraphicsView::mouseMoveEvent(), I was not sure how this could work. But it does! :)

          Thank @JonB and @ofmrew! Thank you very much.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved