我正在尝试实施两个选项;首先,当改变窗口大小时,我的绘图填充了白色,我得到了它。
#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();
}
这是我的drawplace.cpp @eyllanesc
并且它有效,但第二个选项是我的绘图不能消失并保持在同一个地方,但窗口正在改变它的大小......我不知道该怎么做。
我尝试使用drawImage和其他人,但它不起作用。
答案 0 :(得分:1)
根据您的说法,我了解您正在删除使用鼠标绘制的线条,这是因为您正在放置具有白色背景的图像并替换resizeEvent
方法中的上一个图像,你必须做的也是复制图纸。我建议的解决方案只是在新尺寸在某些尺寸上更大并重新绘制时进行绘制。
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();
}
}