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. Why do I not get any MouseEvents in a QGraphicsItem derived class?
Qt 6.11 is out! See what's new in the release blog

Why do I not get any MouseEvents in a QGraphicsItem derived class?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 2 Posters 5.7k Views 1 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #2

    Hi! The following works with Qt 5.9. It prints "Item" if you click on a custom item, and it prints "Nothing" if you click on empty space in the custom view.

    myview.h

    #ifndef MYVIEW_H
    #define MYVIEW_H
    
    #include <QGraphicsView>
    
    class MyView : public QGraphicsView
    {
    public:
        MyView(QWidget *parent = Q_NULLPTR);
    protected:
        virtual void mousePressEvent(QMouseEvent *event) override;
    };
    
    #endif // MYVIEW_H
    

    myview.cpp

    #include "myview.h"
    #include <QMouseEvent>
    #include <QGraphicsItem>
    #include <QDebug>
    
    MyView::MyView(QWidget *parent)
        : QGraphicsView(parent)
    {
    }
    
    void MyView::mousePressEvent(QMouseEvent *event)
    {
        if (!itemAt(event->pos())) {
            qDebug("Nothing");
        }
        QGraphicsView::mousePressEvent(event);
    }
    

    myitem.h

    #ifndef MYITEM_H
    #define MYITEM_H
    
    #include <QGraphicsItem>
    
    class MyItem : public QGraphicsItem
    {
    public:
        MyItem(QGraphicsItem *parent = Q_NULLPTR);
        virtual QRectF boundingRect() const override;
        virtual void paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *) override;
    protected:
        virtual void mousePressEvent(QGraphicsSceneMouseEvent *) override;
    };
    
    #endif // MYITEM_H
    

    myitem.cpp

    #include "myitem.h"
    #include <QPainter>
    #include <QDebug>
    
    MyItem::MyItem(QGraphicsItem *parent)
        : QGraphicsItem(parent)
    {
    }
    
    QRectF MyItem::boundingRect() const
    {
        auto const penWidth = 1.0;
        return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, 20 + penWidth, 20 + penWidth);
    }
    
    void MyItem::paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *)
    {
        painter->setBrush(QBrush(QColor("orange")));
        painter->setPen(QColor("lime"));
        painter->drawRect(-10, -10, 20, 20);
    }
    
    void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *)
    {
        qDebug() << "Item";
    }
    
    NiagarerN 1 Reply Last reply
    3
    • ? A Former User

      Hi! The following works with Qt 5.9. It prints "Item" if you click on a custom item, and it prints "Nothing" if you click on empty space in the custom view.

      myview.h

      #ifndef MYVIEW_H
      #define MYVIEW_H
      
      #include <QGraphicsView>
      
      class MyView : public QGraphicsView
      {
      public:
          MyView(QWidget *parent = Q_NULLPTR);
      protected:
          virtual void mousePressEvent(QMouseEvent *event) override;
      };
      
      #endif // MYVIEW_H
      

      myview.cpp

      #include "myview.h"
      #include <QMouseEvent>
      #include <QGraphicsItem>
      #include <QDebug>
      
      MyView::MyView(QWidget *parent)
          : QGraphicsView(parent)
      {
      }
      
      void MyView::mousePressEvent(QMouseEvent *event)
      {
          if (!itemAt(event->pos())) {
              qDebug("Nothing");
          }
          QGraphicsView::mousePressEvent(event);
      }
      

      myitem.h

      #ifndef MYITEM_H
      #define MYITEM_H
      
      #include <QGraphicsItem>
      
      class MyItem : public QGraphicsItem
      {
      public:
          MyItem(QGraphicsItem *parent = Q_NULLPTR);
          virtual QRectF boundingRect() const override;
          virtual void paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *) override;
      protected:
          virtual void mousePressEvent(QGraphicsSceneMouseEvent *) override;
      };
      
      #endif // MYITEM_H
      

      myitem.cpp

      #include "myitem.h"
      #include <QPainter>
      #include <QDebug>
      
      MyItem::MyItem(QGraphicsItem *parent)
          : QGraphicsItem(parent)
      {
      }
      
      QRectF MyItem::boundingRect() const
      {
          auto const penWidth = 1.0;
          return QRectF(-10 - penWidth / 2, -10 - penWidth / 2, 20 + penWidth, 20 + penWidth);
      }
      
      void MyItem::paint(QPainter *painter, QStyleOptionGraphicsItem const *, QWidget *)
      {
          painter->setBrush(QBrush(QColor("orange")));
          painter->setPen(QColor("lime"));
          painter->drawRect(-10, -10, 20, 20);
      }
      
      void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *)
      {
          qDebug() << "Item";
      }
      
      NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by
      #3

      @Wieland
      Ok, thank you!
      I tried it in my project, it didn't work in version 5.8 so I downloaded 5.9.1 and now I'm unfortunately having problems with the Qt version. It says, this is Qt version 5.9.1 for Android (?) and it can't build and run my project. I downloaded and installed the normal version, not for Android... Phew I don't know, I will look and update when I have it (I hate it when I can't continue a project because of such unnessecary problems -.- )

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #4

        I'm sorry to hear that :-/ But I'm sure it works with 5.8, too. I only mentioned 5.9 because I thought you might be using 4.7 or older.

        NiagarerN 2 Replies Last reply
        1
        • ? A Former User

          I'm sorry to hear that :-/ But I'm sure it works with 5.8, too. I only mentioned 5.9 because I thought you might be using 4.7 or older.

          NiagarerN Offline
          NiagarerN Offline
          Niagarer
          wrote on last edited by
          #5
          This post is deleted!
          1 Reply Last reply
          0
          • ? A Former User

            I'm sorry to hear that :-/ But I'm sure it works with 5.8, too. I only mentioned 5.9 because I thought you might be using 4.7 or older.

            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by Niagarer
            #6

            @Wieland
            Ok, I reinstalled it and added the things I needed (I hope) and made a new project with your code (and some additional lines) and it worked. But my big project still does not work and so far, I cant find the mistake. However I tried to do the procedure nearly the same as in your example, it does not work yet

            1 Reply Last reply
            0
            • NiagarerN Offline
              NiagarerN Offline
              Niagarer
              wrote on last edited by Niagarer
              #7
              This post is deleted!
              1 Reply Last reply
              0
              • NiagarerN Offline
                NiagarerN Offline
                Niagarer
                wrote on last edited by
                #8
                This post is deleted!
                1 Reply Last reply
                0
                • NiagarerN Offline
                  NiagarerN Offline
                  Niagarer
                  wrote on last edited by
                  #9
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • NiagarerN Offline
                    NiagarerN Offline
                    Niagarer
                    wrote on last edited by Niagarer
                    #10
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • NiagarerN Offline
                      NiagarerN Offline
                      Niagarer
                      wrote on last edited by
                      #11

                      The final answer for all these problems is there:
                      https://forum.qt.io/topic/82085/qgraphicsview-mousereleaseevent-ensures-that-i-can-t-select-qgraphicsitems-anymore/2

                      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