我正在尝试实现两种选择;首先,当更改窗口大小时,我的图形将填充为白色,然后得到了。
#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和其他方法,但是它不起作用。
最佳答案
根据您所说的内容,我了解您正在擦除用鼠标绘制的线条,并且发生这种情况是因为您要放置带有白色背景的图像并替换为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();
}
}
关于c++ - Qt, “Paint”中的resizeEvent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48509885/