Should I use QDialog widget to draw line?
-
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 tovoid 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.
-
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?
-
@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). -
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.