But if I make the overlay-ed pixmap transparent then user can't see what he draws ?
If I make it opaque then user can't see the base image ?
How to make the user see the overlay where he draws and background where he hasn't drawn ?
Thanks,
Charvi
Hi,
The widget must not be transparent but the pixels on it must have the Alpha value to full transparency. When you draw on int you have to change the Alpha value and the RGB values to desired color.
Òscar Llarch i Galán
Geez....
Qt Code:
#include <QtWidgets> public: if(!m_mask.isNull()) m_mask.fill(Qt::transparent); updateGeometry(); } protected: if(!m_base.rect().contains(e->pos())) return; m_mask.setPixel(e->pos(), qRgba(255, 0, 0, 255)); update(); } if(!m_base.rect().contains(e->pos())) return; m_mask.setPixel(e->pos(), qRgba(255, 0, 0, 255)); update(); } p.drawPixmap(0, 0, m_base); p.drawImage(0, 0, m_mask); } private: QPixmap m_base; QImage m_mask; }; int main(int argc, char **argv) { Widget w; w.setImage("/usr/share/icons/oxygen/256x256/apps/kword.png"); w.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
Charvi (18th July 2015)
Also, how would i construct QPen to erase the overlay ?
Why would you need a pen for that?
You take the code wysota already wrote for you, and you replace the alpha value with 0 instead of 255 in the setPixel() calls.Also, how would i construct QPen to erase the overlay ?
You seem to be confused about the difference between the virtual "pen", "eraser" and other tools you might use in a drawing application and a QPen used by QPainter. Totally unrelated concepts.
To get smooth drawing I used the following code taking reference from the Scribble example.
Qt Code:
// if (eraseFlag == true) // painter.setPen(QPen(QBrush(QColor(0, 0, 0, 0)), myPenWidth, Qt::SolidLine, Qt::RoundCap, // Qt::RoundJoin)); // else Qt::RoundJoin)); painter.drawLine(lastPoint, endPoint); int rad = (myPenWidth / 2) + 2; .adjusted(-rad, -rad, +rad, +rad)); lastPoint = endPoint;To copy to clipboard, switch view to plain text mode
where
Qt Code:
myPenWidth = 3; myPenColor = Qt::red;To copy to clipboard, switch view to plain text mode
Trying to implement erasing in this.
One more feature, sorry to extend in the same question but it's because the context has already been set.
In order to implement scaling on the wheel event I did the following:
Qt Code:
void { scaleFactor += ((float)event->delta()/1200.0); update(); }To copy to clipboard, switch view to plain text mode
and in the paintEvent:
Qt Code:
void { p.scale(scaleFactor, scaleFactor); p.drawPixmap(0, 0, m_base); p.drawImage(0, 0, m_mask); }To copy to clipboard, switch view to plain text mode
My widget hierarchy is as below:
Mainwindow
- Grid Layout
- Scrollarea
- MyCustomDrawing Canvas Widget (created just like the code wysota suggested above)
I am not getting the scrollbars on scaling the image. I realize I am drawing the scaled image again, instead of resizing the widget. I tried using resize on the widget but it didn't work. What I am doing wrong ?
Set the widgetResizable property of the scroll area to true.
Okay so for erasing I did the following, may be this will be useful to someone.
Qt Code:
To copy to clipboard, switch view to plain text mode
To understand the Composition Modes : http://doc.qt.io/qt-5/qpainter.html#...itionMode-enum
Thanks.
P.S.: Still working on the scrollarea stuff. Once done will update here.
I think it might be faster to use composition mode CLEAR. It erases whatever pixels are there.
Bookmarks