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. Draw line on mouse click
Forum Updated to NodeBB v4.3 + New Features

Draw line on mouse click

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 4.1k 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.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on last edited by behruz montazeri
    #1

    What's the problem. It doesn't respond my mouse click and release. It just draw a red point.
    Is it possible to call painter outside of paint function ?

    void MyPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
    
       QPen pen;
       pen.setWidth(10);
       pen.setColor(QColor(Qt::red));
       painter->setPen(pen);
       painter->drawLine(mLine);
    
    
    }
    
    
    void MyPoly::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        startPoint = event->scenePos().toPoint();
        QGraphicsItem::mousePressEvent(event);
        update();
    
    }
    
    void MyPoly::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        endPoint = event->scenePos().toPoint();
        mLine.setPoints(startPoint,endPoint);
        QGraphicsItem::mouseReleaseEvent(event);
        update();
    }
    
    
    Gojir4G 1 Reply Last reply
    0
    • behruz montazeriB behruz montazeri

      What's the problem. It doesn't respond my mouse click and release. It just draw a red point.
      Is it possible to call painter outside of paint function ?

      void MyPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
      
         QPen pen;
         pen.setWidth(10);
         pen.setColor(QColor(Qt::red));
         painter->setPen(pen);
         painter->drawLine(mLine);
      
      
      }
      
      
      void MyPoly::mousePressEvent(QGraphicsSceneMouseEvent *event)
      {
          startPoint = event->scenePos().toPoint();
          QGraphicsItem::mousePressEvent(event);
          update();
      
      }
      
      void MyPoly::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
      {
          endPoint = event->scenePos().toPoint();
          mLine.setPoints(startPoint,endPoint);
          QGraphicsItem::mouseReleaseEvent(event);
          update();
      }
      
      
      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by Gojir4
      #2

      @behruz-montazeri Hi,

      This is because of QGraphicsItem::mousePressEvent(event);. It should work if you remove this line.

      From the doc of QGraphicsItem::mousePressEvent:

      The mouse press event decides which item should become the mouse grabber (see QGraphicsScene::mouseGrabberItem()). If you do not reimplement this function, the press event will propagate to any topmost item beneath this item, and no other mouse events will be delivered to this item.

      If you do reimplement this function, event will by default be accepted (see QEvent::accept()), and this item is then the mouse grabber. This allows the item to receive future move, release and doubleclick events. If you call QEvent::ignore() on event, this item will lose the mouse grab, and event will propagate to any topmost item beneath. No further mouse events will be delivered to this item unless a new mouse press event is received.```

      Here is what I have used for testing ( I have also added drawing line when mouse is moving and pressed):

      class MyPoly : public QGraphicsItem{
          QPoint startPoint;
          QLine m_line;
          bool pressed = false;
      protected:
          void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
          {
      
             QPen pen;
      
             pen.setWidth(2);
             pen.setColor(QColor(Qt::darkGray));
             painter->setPen(pen);
             painter->drawRect(boundingRect().adjusted(5, 5, -5, -5));
      
             if(m_line.isNull()) return;
             pen.setWidth(10);
             pen.setColor(QColor(Qt::red));
             painter->setPen(pen);
             painter->drawLine(m_line);
      
      
          }
      
      
          QRectF boundingRect() const{
              return QRectF(QPointF(0,0), QPointF(200, 200));
          }
      
          void mousePressEvent(QGraphicsSceneMouseEvent *event)
          {
              startPoint = event->scenePos().toPoint();
              qDebug() << "Pressed, start: " << startPoint;
              pressed = true;
              update();
              
              //QGraphicsItem::mousePressEvent(event);
      
          }
      
          void mouseMoveEvent(QGraphicsSceneMouseEvent *event){
              if(pressed){
                  QPoint endPoint = event->scenePos().toPoint();
                  qDebug() << "Moving, start: " << startPoint << "  end: " << endPoint;
                  m_line.setPoints(startPoint, endPoint);
                  update();
              }
              QGraphicsItem::mousePressEvent(event);
          }
      
          void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
          {
              pressed = false;
              QPoint endPoint = event->scenePos().toPoint();
              m_line.setPoints(startPoint, endPoint);
              qDebug() << "Release, start: " << startPoint << "  end: " << endPoint;
              update();
              QGraphicsItem::mouseReleaseEvent(event);
          }
      };
      
      1 Reply Last reply
      5
      • behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by
        #3

        @Gojir4
        Thanks. It really helps me. One of the my issues was not defining the QRectF boundingRect.

        Gojir4G 1 Reply Last reply
        0
        • behruz montazeriB behruz montazeri

          @Gojir4
          Thanks. It really helps me. One of the my issues was not defining the QRectF boundingRect.

          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on last edited by
          #4

          @behruz-montazeri Glad it helps ! Can you please set the topic solved.

          1 Reply Last reply
          2

          • Login

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