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. Drawing a line dynamically on QtWidget
Forum Updated to NodeBB v4.3 + New Features

Drawing a line dynamically on QtWidget

Scheduled Pinned Locked Moved General and Desktop
guic++
5 Posts 2 Posters 4.9k 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.
  • S Offline
    S Offline
    simpleProgrammer777
    wrote on last edited by p3c0
    #1

    Hi, Here is a code to draw a line on widget. Problem is it's keeping the previously drawn line and on each mouse move it's drawing again and again. I want to draw like mspaint i.e only on mouse release event it will finalize the line drawing (otherwise just preview of lines). One idea I thought about is to delete the preview lines on each mouse move, other is to draw on some temporary view or something and on mouse release finalize it. But having difficulty finding a proper way how to do it.
    Any help is highly appreciated. Thanks

    #include "paintwidget.h"
    #include "ui_paintwidget.h"
    
    #include <QtGui>
    
    paintWidget::paintWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::paintWidget)
    {
        ui->setupUi(this);
    
        m_nInitialX = 0;
        m_nInitialY = 0;
        m_nFinalX = 0;
        m_nFinalY = 0;
        m_nPTargetPixmap = 0;
        m_nPTargetPixmap = new QPixmap(400,400);
        m_nbMousePressed = false;
    }
    
    paintWidget::~paintWidget()
    {
        delete ui;
    }
    
    void paintWidget::mousePressEvent(QMouseEvent* event)
    {
        m_nbMousePressed = true;
        m_nInitialX = event->pos().x();
        m_nInitialY = event->pos().y();
    }
    
    void paintWidget::mouseReleaseEvent(QMouseEvent *event)
    {
        m_nbMousePressed = false;
        //update();
    }
    
    void paintWidget::paintEvent(QPaintEvent *e)
    {
        if(m_nbMousePressed)
        {
            QPainter PixmapPainter(m_nPTargetPixmap);
            QPen pen(Qt::green);
            PixmapPainter.setPen(pen);
            //PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
        }
        QPainter painter(this);
        painter.drawPixmap(0, 0, *m_nPTargetPixmap);
    }
    
    void paintWidget::mouseMoveEvent(QMouseEvent *event)
    {
        if (event->type() == QEvent::MouseMove)
        {
            QPainter PixmapPainter(m_nPTargetPixmap);
            QPen pen(Qt::black);
            PixmapPainter.setPen(pen);
            PixmapPainter.drawLine(m_nInitialX, m_nInitialY, m_nFinalX, m_nFinalY);
            update(); // update your view
            m_nFinalX = event->pos().x();
            m_nFinalY = event->pos().y();
        }
        update(); // update your view
    }
    

    Edited: Use ``` (3 backticks) instead - p3c0

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SysTech
      wrote on last edited by
      #2

      I have not tried to run your code but generally what this involves is on the current line that you are previewing you have to do pretty much the following

      first time - draw it but remember/store what line you drew
      detect if move, if moved XOR draw the saved line (erases it), then draw the new line and save it.

      So you sort of need to keep a temp history of the last line you drew before releasing the mouse. If your mouse moves then you must know if you drew a preview line and XOR draw it.

      I have not used QPainter yet so I'm not able to give you the calls to make but the basic concept is if you drew a line in red, you need to undo that line either by redrawing it in your background color or a color that is the XOR of red.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        simpleProgrammer777
        wrote on last edited by
        #3

        @SysTech Thanks I understand your idea. But how do I XOR a drawn line?.. Redrawing in background color might not be feasible because there could be other shapes drawn underneath the current drawing object.

        S 1 Reply Last reply
        0
        • S simpleProgrammer777

          @SysTech Thanks I understand your idea. But how do I XOR a drawn line?.. Redrawing in background color might not be feasible because there could be other shapes drawn underneath the current drawing object.

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

          @simpleProgrammer777
          Like I said I have not used QPainter (yet)... so I don't have the calls right handy. Hopefully someone else will chime in.

          If I get time I will take a look.

          One thing I might suggest is to take a look at the Analog Clock example program. This appears to me to do a complete redraw but it's very fast. You might be able to make use of that technique.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            simpleProgrammer777
            wrote on last edited by
            #5

            I have a solution thanks :)

            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