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. Custom QGraphicsView items are not movable

Custom QGraphicsView items are not movable

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewqgraphicsitemqgraphicsellips
2 Posts 2 Posters 730 Views 2 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.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on last edited by
    #1

    I made my own QGraphicsView, but the items I've created are not movable.

    this is the code:

    #include "graphicsview.h"
    
    GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent)
    {
        scene = new QGraphicsScene();
        this->setScene(scene);
        this->setAlignment(Qt::AlignLeft | Qt::AlignTop);
        LoadImage();
    }
    
    void GraphicsView::mousePressEvent(QMouseEvent *event)
    {
        if(event->buttons().testFlag(Qt::LeftButton))
        {
            int x,y;
            x = event->pos().x();
            y = event->pos().y();
            qDebug() << x << ", " << y;
            QPointF point = mapToScene(x, y);
            QGraphicsEllipseItem *ellipse;
            ellipse = scene->addEllipse(point.x(),point.y(),5,5,QPen(Qt::red), QBrush(Qt::red));
            ellipse->setFlag(QGraphicsEllipseItem::ItemIsMovable);
        }
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      I think you just broke GraphicsView normal handling.
      Try also calling base class mousePressEvent as it might use for the actual item selection

      void GraphicsView::mousePressEvent(QMouseEvent *event)
      {
          if(event->buttons().testFlag(Qt::LeftButton))
          {
              int x,y;
              x = event->pos().x();
              y = event->pos().y();
              qDebug() << x << ", " << y;
              QPointF point = mapToScene(x, y);
              QGraphicsEllipseItem *ellipse;
              ellipse = scene->addEllipse(point.x(),point.y(),5,5,QPen(Qt::red), QBrush(Qt::red));
              ellipse->setFlag(QGraphicsEllipseItem::ItemIsMovable);
          }
      // call base class
      QGraphicsView::mousePressEvent(event);
      }
      
      1 Reply Last reply
      3

      • Login

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