Reputation: 81
I am trying to implement two options; First when changing a window size my drawing is filling in white color, and I got it.
#include "drawPlace.h"
#include "QDebug"
#include "QFile"
#include "QFileDialog"
#include "QString"
#include "QPen"
#include "QPainter"
drawPlace::drawPlace(QWidget *parent) : QWidget(parent)
{
}
void drawPlace::drawBlue()
{
qDebug("blue");
mColour = Qt::blue;
}
void drawPlace::drawRed()
{
qDebug("red");
mColour = Qt::red;
}
void drawPlace::drawYellow()
{
qDebug("yellow");
mColour = Qt::yellow;
}
void drawPlace::drawGreen()
{
qDebug("green");
mColour = Qt::green;
}
void drawPlace::clearScreen()
{
qDebug("CLEAR");
QImage bitMap(this->size(), QImage::Format_RGB32);
bitMap.fill(Qt::white);
mDraw = bitMap;
this->update();
}
bool drawPlace::saveFile()
{
qDebug("SAVING");
mDraw.save(QFileDialog::getSaveFileName(this, ("Save File"), "/home/jana/untitled.png", "Pictures (*.PNG)"));
return true;
}
void drawPlace::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
qDebug("Drawing started");
mDrawing = true;
oldPos = event->pos();
}
}
void drawPlace::mouseMoveEvent(QMouseEvent *event)
{
if(mDrawing)
{
QPen newPen(mColour, 3);
QPainter drawing(&mDraw);
drawing.setPen(newPen);
newPos = event->pos();
drawing.drawLine(oldPos, newPos);
oldPos = newPos;
this->update();
}
}
void drawPlace::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
qDebug("Drawing ended");
mDrawing = false;
}
}
void drawPlace::paintEvent(QPaintEvent *event)
{
QPainter drawing(this);
drawing.drawImage(0, 0, mDraw);
}
void drawPlace::resizeEvent(QResizeEvent *event)
{
QImage newDraw(event>size(), QImage::Format_RGB32);
newDraw.fill(Qt::white);
mDraw = newDraw;
this->update();
}
it's my drawplace.cpp @eyllanesc
and it works, but the second option is that my drawing can't disappear and stay in the same place, but window is changing its size... I got no idea how to do it.
I tried with drawImage and others, but it don't work.
Upvotes: 1
Views: 1153
Reputation: 244301
According to what you say, I understand that you are erasing the lines you draw with the mouse, and that happens because you are placing an image with a white background and replacing the previous image in the resizeEvent
method, what you must do is also copy the drawing. The solution that I propose is only to paint if the new size is bigger in some of its dimensions and repaint.
void drawPlace::resizeEvent(QResizeEvent *event)
{
if(event->size().width() > mDraw.size().width() || event->size().height() > mDraw.size().height()){
QSize size;
size.setWidth(event->size().width() > mDraw.size().width()? event->size().width(): mDraw.size().width());
size.setHeight(event->size().height() > mDraw.size().height()? event->size().height(): mDraw.size().height());
QImage newDraw(size, QImage::Format_RGB32);
newDraw.fill(Qt::white);
QPainter painter(&newDraw);
painter.drawImage(QPoint(), mDraw);
painter.end();
mDraw = newDraw;
update();
}
}
Upvotes: 1