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. Need help with QGraphicsItem painter
Forum Updated to NodeBB v4.3 + New Features

Need help with QGraphicsItem painter

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 918 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.
  • behruz montazeriB Offline
    behruz montazeriB Offline
    behruz montazeri
    wrote on last edited by
    #1

    Re: Draw line on mouse click
    What's the problem it only draw couple of lines.

    #include "myitem.h"
    #include <QPainter>
    #include <QGraphicsSceneMouseEvent>
    MyItem::MyItem()
    {
        pressed = false;
    }
    
    void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
            painter->drawRect(boundingRect());
            painter->drawLine(mline);
    }
    
    QRectF MyItem::boundingRect() const
    {
        return QRectF(0,0,800,600);
    }
    
    void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
    
        pressed = true;
        startPoint = event->scenePos().toPoint();
        update();
    
    }
    
    void MyItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {
        if(!pressed)
        {
           event->accept();
           return;
        }
        if(pressed){
            endtPoint = event->scenePos().toPoint();
            mline.setPoints(startPoint,endtPoint);
            update();
            startPoint=endtPoint;
            event->accept();
    
        }
        QGraphicsItem::mouseMoveEvent(event);
    }
    
    void MyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
            pressed = false;
            event->accept();
            update();
            QGraphicsItem::mouseReleaseEvent(event);
    }
    
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      drawLine() only draws one line so what do you expect? See http://doc.qt.io/qt-5/qpainter.html#drawLine
      Maybe you're looking for drawLines() ? -> http://doc.qt.io/qt-5/qpainter.html#drawLines-1

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      behruz montazeriB 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        drawLine() only draws one line so what do you expect? See http://doc.qt.io/qt-5/qpainter.html#drawLine
        Maybe you're looking for drawLines() ? -> http://doc.qt.io/qt-5/qpainter.html#drawLines-1

        behruz montazeriB Offline
        behruz montazeriB Offline
        behruz montazeri
        wrote on last edited by behruz montazeri
        #3

        @Christian-Ehrlicher
        I've checked scribble example and it also uses drawLine.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @behruz-montazeri said in Need help with QGraphicsItem painter:

          I've checked scribble example and it also uses drawLine.

          It paints on an image without clearing it before drawing a new line. You're painting directly on a widget inside the paint event so the old content is erased.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          behruz montazeriB 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            @behruz-montazeri said in Need help with QGraphicsItem painter:

            I've checked scribble example and it also uses drawLine.

            It paints on an image without clearing it before drawing a new line. You're painting directly on a widget inside the paint event so the old content is erased.

            behruz montazeriB Offline
            behruz montazeriB Offline
            behruz montazeri
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            What's the workaround ?
            I've selected the wrong widget ? i mean QGraphicsItem ?

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              I already told you what you can use, or do it the same way it's done in the scribble example.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi
                The scribble sample paints on an image so all older lines are still shown as
                each new line is added to this image.

                In your case, you draw one line and store it in mline
                If you want it to manage more than one line, you need a list
                QVector<QLineF> mylines;
                and each time you create a new line , you append it to mylines.
                Then in paintEvent
                You call
                drawLines(mylines);

                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