c++ - Qt : Create a Stack of image

标签 c++ qt stack qimage

我是 Qt 新手,目前正在尝试设计一个简单的画线应用程序。我设法使用 QImage 画线和 MouseEvent (该线从鼠标单击开始,到鼠标释放结束)。

现在我想创建一条出现在mouseMoveEvent期间的“幽灵”线。仅有的。我想使用 QImage 的堆栈(这将允许我稍后管理撤消重做)来做到这一点。但是,我什至无法构建堆栈,程序崩溃了,没有任何解释

这是我在 header 类中的声明

private:
    QImage image;
    QStack <QImage> *history

这是我的构造函数

Painty::Painty() : image(1920,1080, QImage::Format_ARGB32)
{
    image.fill(Qt::white);
    history = new QStack <QImage>;
}

这是我的功能:

void Painty::mousePressEvent(QMouseEvent *event)
{
     f_point = event->pos();
}

 void Painty::mouseReleaseEvent(QMouseEvent *event)
 {
    l_point = event->pos();
    addLine();
    history->push(image);
 }

 void Painty::mouseMoveEvent(QMouseEvent *event)
 {
    l_point = event->pos();
    addLine();
 }


 void Painty::paintEvent(QPaintEvent *event)
 {
    QWidget::paintEvent(event);
    QPainter painter(this);
    painter.drawImage(0,0,image);
 }

void Painty::addLine() 
{
     image=history->top();
     QPainter paint(&image);
     paint.drawLine(f_point,l_point);
     paint.end();
     this->update();
}

我尝试尽可能多地进行调试,但我所能意识到的是导致程序崩溃的行是 history = new QStack <QImage>;但我不知道它出了什么问题。

最佳答案

您需要将空图像插入构造函数末尾的堆栈,或者交换行

addLine();
history->push(image);

mouseReleaseEvent(...)中。

否则第一次调用 addLine() history->top() 将无效。

关于c++ - Qt : Create a Stack of image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32987360/

相关文章:

C++访问另一个类的数据成员

c++ - Protobuf 版本与 Qt 冲突

c++ - gluPickMatrix - 出了点问题,一个对象总是在现场

linux - 从崩溃信息中打印函数名称

java - 自制堆栈等于方法

c# - 可观察的堆栈和队列

c++ - 解释以下输出?

c++ - 如何检测鼠标点击 QLineEdit

c++ - 如何标记cpp源?

c++ - 无锁引用计数和 C++ 智能指针