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. Should I use QDialog widget to draw line?
Forum Updated to NodeBB v4.3 + New Features

Should I use QDialog widget to draw line?

Scheduled Pinned Locked Moved Solved General and Desktop
draw line
24 Posts 4 Posters 2.8k 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.
  • B Offline
    B Offline
    Bonnie
    wrote on last edited by
    #21

    Oh, if you need QScrollArea + zoom, I'm afraid QGraphics* classes are more suitable for that.
    For current method, I don't know if this will take any affect, but you can try not scaling the pixmap, but the painter.
    Change your paintEvent to

    void DrawingLabel::paintEvent(QPaintEvent *event)
    {
            QPainter painter(this);
            painter.scale(m_zoomRatio, m_zoomRatio);
            painter.drawPixmap(0,0,m_drawingPixmap);
    }
    

    And in your wheelEvent, dont' create and set new pixmap, just set the zoomed fixed size.

    L 1 Reply Last reply
    0
    • B Bonnie

      Oh, if you need QScrollArea + zoom, I'm afraid QGraphics* classes are more suitable for that.
      For current method, I don't know if this will take any affect, but you can try not scaling the pixmap, but the painter.
      Change your paintEvent to

      void DrawingLabel::paintEvent(QPaintEvent *event)
      {
              QPainter painter(this);
              painter.scale(m_zoomRatio, m_zoomRatio);
              painter.drawPixmap(0,0,m_drawingPixmap);
      }
      

      And in your wheelEvent, dont' create and set new pixmap, just set the zoomed fixed size.

      L Offline
      L Offline
      lansing
      wrote on last edited by lansing
      #22

      @Bonnie

      Hi, setting the scale factor inside paintEvent() solved the lagging issue. But now I have problem mapping to the scaled position on the drawing. I tried multiplying the x, y with the same zoom ratio, but they still doesn't line up when I tried to draw. What did I do wrong?

      void DrawingLabel::mousePressEvent(QMouseEvent *a_pEvent)
      {
          if (a_pEvent->button() == Qt::LeftButton) {
              m_drawing = true;
      
              double x = double(a_pEvent->pos().x()) * m_zoomRatio;
              double y = double(a_pEvent->pos().y()) * m_zoomRatio;  
              m_lastPoint = QPointF(x, y);
          }
      }
      
      void DrawingLabel::mouseMoveEvent(QMouseEvent *a_pEvent)
      {
          if ((a_pEvent->buttons() & Qt::LeftButton) && m_drawing) {
              double x = double(a_pEvent->pos().x()) * m_zoomRatio;
              double y = double(a_pEvent->pos().y()) * m_zoomRatio;
              QPointF zoomPos(x,y);
      
              drawPointTo(zoomPos);
          } else {
              QWidget::mouseMoveEvent(a_pEvent);
          }
      }
      
      void DrawingLabel::drawPointTo(const QPointF &a_endPoint)
      {
          QPainter painter(&m_drawingPixmap);
          painter.setPen(QPen(Qt::black, 2));
      
          painter.drawLine(m_lastPoint, a_endPoint);
          update();
          m_lastPoint = a_endPoint;
      }
      

      Also a cosmetic problem, the drew line was also scaled with the pixmap, making it look pixelated when zoomed in, how do I make its look independent from the zoom?

      B 1 Reply Last reply
      0
      • L lansing

        @Bonnie

        Hi, setting the scale factor inside paintEvent() solved the lagging issue. But now I have problem mapping to the scaled position on the drawing. I tried multiplying the x, y with the same zoom ratio, but they still doesn't line up when I tried to draw. What did I do wrong?

        void DrawingLabel::mousePressEvent(QMouseEvent *a_pEvent)
        {
            if (a_pEvent->button() == Qt::LeftButton) {
                m_drawing = true;
        
                double x = double(a_pEvent->pos().x()) * m_zoomRatio;
                double y = double(a_pEvent->pos().y()) * m_zoomRatio;  
                m_lastPoint = QPointF(x, y);
            }
        }
        
        void DrawingLabel::mouseMoveEvent(QMouseEvent *a_pEvent)
        {
            if ((a_pEvent->buttons() & Qt::LeftButton) && m_drawing) {
                double x = double(a_pEvent->pos().x()) * m_zoomRatio;
                double y = double(a_pEvent->pos().y()) * m_zoomRatio;
                QPointF zoomPos(x,y);
        
                drawPointTo(zoomPos);
            } else {
                QWidget::mouseMoveEvent(a_pEvent);
            }
        }
        
        void DrawingLabel::drawPointTo(const QPointF &a_endPoint)
        {
            QPainter painter(&m_drawingPixmap);
            painter.setPen(QPen(Qt::black, 2));
        
            painter.drawLine(m_lastPoint, a_endPoint);
            update();
            m_lastPoint = a_endPoint;
        }
        

        Also a cosmetic problem, the drew line was also scaled with the pixmap, making it look pixelated when zoomed in, how do I make its look independent from the zoom?

        B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #23

        @lansing
        Well, I think you don't need to map the points in the mouse events.
        Just set the painter of the pixmap to be scaled reversely.
        And in order to keep the line width, set the pen width accordingly.

            QPainter painter(&m_drawingPixmap);
            painter.scale(1/m_zoomRatio, 1/m_zoomRatio);
            painter.setPen(QPen(Qt::black, 2/m_zoomRatio));
            painter.drawLine(lastPoint, endPoint);
        

        But I feel it look not "smooth"...

        If this line width issue can't be solved, then you should use back your old way, but try to optimize the wheelEvent part.
        I think the lagging issue is caused by scaling be called rapidly in a short time (from several wheel events).

        L 1 Reply Last reply
        0
        • B Bonnie

          @lansing
          Well, I think you don't need to map the points in the mouse events.
          Just set the painter of the pixmap to be scaled reversely.
          And in order to keep the line width, set the pen width accordingly.

              QPainter painter(&m_drawingPixmap);
              painter.scale(1/m_zoomRatio, 1/m_zoomRatio);
              painter.setPen(QPen(Qt::black, 2/m_zoomRatio));
              painter.drawLine(lastPoint, endPoint);
          

          But I feel it look not "smooth"...

          If this line width issue can't be solved, then you should use back your old way, but try to optimize the wheelEvent part.
          I think the lagging issue is caused by scaling be called rapidly in a short time (from several wheel events).

          L Offline
          L Offline
          lansing
          wrote on last edited by
          #24

          @Bonnie

          Hi yes this doesn't work well, the drew line get softer and softer with more zoom, and it gets bigger when zoomed out. I have to revert back to the wheelEvent controlled zoom.

          Oh and I just observed that the lagging issue are also presented on Photoshop too. When the zoom factor get to about 7x, zoom will get less responsive. So I'm all good now, if even Photoshop is having the same problem, that's nothing more I can do about it. I'll call this a day.

          Thanks for the help.

          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