@YankaKupala

Как сохранять предыдущие изображения в qt?

Мне нужно сделать графический редактор вроде paint но мои предыдущие рисунки не сохраняются т.е. если я рисую 2ой прямоугольник то 1ый пропадает
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
 ,m_isDrawing(false)
    , m_shapeType(0)
{

     setMouseTracking(true);
    m_plblX = new QLabel(this);
    m_plblY = new QLabel(this);
    statusBar()->addWidget(m_plblY);
    statusBar()->addWidget(m_plblX);
    line=new QAction(tr("&Линия"),this);
    connect(line,SIGNAL(triggered()),this,SLOT(Line()));
    ellipse = new QAction(tr("&Эллипс"), this);
    connect(ellipse, SIGNAL(triggered()), this, SLOT(Ellips()));
    rectangle = new QAction(tr("&Прямоугольник"), this);
    connect(rectangle, SIGNAL(triggered()), this, SLOT(Rectangle()));
    fileMenu = this->menuBar()->addMenu(tr("&Выбор фигуры"));
    fileMenu->addAction(line);
    fileMenu->addAction(rectangle);
    fileMenu->addAction(ellipse);


}

MainWindow::~MainWindow()
{
    delete ui;

}
void MainWindow::Line()
{
    m_shapeType=1;
}
void MainWindow::Ellips()
{
    m_shapeType=2;
}
void MainWindow::Rectangle()
{
    m_shapeType=3;
}
struct MainWindow::qp{
    int x,y;
    int x_end,y_end;
    int figure;
    bool f;
};
void MainWindow::penColor()

{

    // Store the chosen color from the dialog

   



    // If a valid color set it



}

void MainWindow::setPointFigure(int figure)
{
    qp qp1;
    qp1.x=m_startPoint.x();
    qp1.y= m_startPoint.y();
    qp1.x_end=m_endPoint.x();
    qp1.y_end=m_endPoint.y();
    qp1.figure=figure;
    vcp.append(qp1);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_startPoint = event->pos();
        m_isDrawing = true;
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton && m_isDrawing)
    {
        m_endPoint = event->pos();
        m_isDrawing = false;

        update();
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isDrawing)
    {
        m_endPoint = event->pos();
        update();
    }
}
void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);



    myPenWidth = 1;

    myPenColor = Qt::blue;
    painter.setPen(Qt::blue);
    // draw all shapes


    // draw current shape being drawn

        if (m_shapeType == 3)
        {

             setPointFigure(3);
            painter.drawRect(m_startPoint.x(), m_startPoint.y(), m_endPoint.x() - m_startPoint.x(), m_endPoint.y() - m_startPoint.y());

        }
        else if (m_shapeType == 1)
        {
setPointFigure(1);
painter.drawLine(m_startPoint.x(), m_startPoint.y(),m_endPoint.x(),m_endPoint.y());

        }
        else if (m_shapeType == 2)
        {
setPointFigure(2);
painter.drawEllipse(m_startPoint.x(), m_startPoint.y(),m_endPoint.x() - m_startPoint.x(), m_endPoint.y() - m_startPoint.y());

        }

for (int i=1;i<vcp.size()+1;++i)
{
    if(!vcp[i-1].f)continue;
    if(vcp[i-1].figure==1) painter.drawLine(vcp[i-1].x,vcp[i-1].y,vcp[i-1].x_end,vcp[i-1].y_end);
    if(vcp[i-1].figure==2) painter.drawEllipse(vcp[i-1].x,vcp[i-1].y,vcp[i-1].x_end-vcp[i-1].x,vcp[i-1].y_end-vcp[i-1].y);
    if(vcp[i-1].figure==3) painter.drawRect(vcp[i-1].x,vcp[i-1].y,vcp[i-1].x_end-vcp[i-1].x,vcp[i-1].y_end-vcp[i-1].y);
}
}

<code lang="cpp">
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QtWidgets>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);

    ~MainWindow();


private:
     Ui::MainWindow *ui;
QMenu *fileMenu;
QAction *line;
QAction *ellipse;
QAction *rectangle;
QLabel* m_plblX;
   QLabel* m_plblY;
int m_shapeType;
   QPoint m_startPoint;
   QPoint m_endPoint;
   bool m_isDrawing;
   QVector<QRect> m_rectangles;
   QVector<QPoint> m_circles;
   struct qp;
QVector<qp> vcp;
int myPenWidth;
QColor myPenColor;

protected:
   virtual void mouseMoveEvent1(QMouseEvent* event)
      {
          m_plblX->setText("X=" + QString().setNum(event->x()));
          m_plblY->setText("Y=" + QString().setNum(event->y()));
      }
   void mousePressEvent(QMouseEvent *event);
   void mouseReleaseEvent(QMouseEvent *event);
   void mouseMoveEvent(QMouseEvent *event);
   void paintEvent(QPaintEvent *event);
 void   setPointFigure(int figure);
public slots:
 void Line();
 void Rectangle();
 void Ellips();
 void penColor();
};
#endif // MAINWINDOW_H

</code>
  • Вопрос задан
  • 50 просмотров
Пригласить эксперта
Ответы на вопрос 1
Вам надо рисовать на QGraphicsScene а не на виджете. Иначе постоянно будут возникать проблемы.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы