找回密码
 立即注册
收起左侧

qt5 点击清空按钮后pixmap没有反应

0
回复
345
查看
[复制链接]

尚未签到

来源: 2021-3-3 15:08:10 显示全部楼层 |阅读模式
1Qter豆
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QObject>
#include <QPixmap>
#include <QPainter>
#include <QPixmap>

class mythread : public QObject
{
    Q_OBJECT
public:
    explicit mythread(QObject *parent = nullptr);


signals:
    void updateImage(QPixmap image_date);

public slots:
    void drawImage(QVector<QPoint>);
    void clear();
private:
    QPixmap pix;
};

#endif // MYTHREAD_H

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QPainter>
#include <QVector>
#include <QPoint>
#include <QMouseEvent>
#include <QPaintEvent>
#include <mythread.h>
#include <QThread>

#include <QMetaType>


namespace Ui {
class MyWidget;
}

class MyWidget : public QWidget
{
    Q_OBJECT

public:
    explicit MyWidget(QWidget *parent = 0);
    void paintEvent(QPaintEvent *ev);


    void mouseMoveEvent(QMouseEvent *ev);//鼠标移动
    void mousePressEvent(QMouseEvent *ev);//鼠标点击

    void getPixmap(QPixmap); //绘制图像的槽函数
    void kill_thread();      //关闭自己创建的子线程



    ~MyWidget();
signals:
    void go_thread(QVector<QPoint>);

private slots:
    void on_clean_clicked();

private:
    Ui::MyWidget *ui;
    QPixmap pixmap;
    QImage image;
    QVector<QPoint> _lines;

    QThread *child_thread;
    mythread *child_object;

};

#endif // MYWIDGET_H


#include "mythread.h"

mythread::mythread(QObject *parent) : QObject(parent)
{
    pix=QPixmap(500,500);
    pix.fill(Qt::transparent);
}

void mythread::drawImage(QVector<QPoint> _lines)
{
    QPainter Painter(&pix);

    QPen pen;
    pen.setWidth(3);

    Painter.setPen(pen);

    for(int j=0;j<_lines.size()-1;j++)
        Painter.drawLine(_lines.at(j),_lines.at(j+1));

           emit updateImage(pix);
}

void mythread::clear()
{
    QPixmap closepix(500,500);
    closepix.fill(Qt::transparent);
    pix=closepix;
}


#include "mywidget.h"
#include "ui_mywidget.h"
#include <QPixmap>
#include <QColor>
#include <QDebug>

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyWidget)
{

     qRegisterMetaType<QVector <QPoint>>("QVector <QPoint>");
     image=QImage(500,500,QImage::Format_RGB32);
     image.fill(Qt::white);

    pixmap = QPixmap(500,500);
    pixmap.fill(Qt::transparent);

    child_object=new mythread;
    child_thread=new QThread;
    child_object->moveToThread(child_thread);
    child_thread->start();

    ui->setupUi(this);

    connect(this,&MyWidget::go_thread,child_object,mythread::drawImage);
    connect(child_object,mythread::updateImage,this,getPixmap);
    connect(this,&MyWidget::destroyed,this, &MyWidget::kill_thread);
    connect(ui->clean,SIGNAL(clicked()),child_object,SLOT(clear()));
}


MyWidget::~MyWidget()
{
    delete ui;
}


void MyWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.drawImage(0,0,image);
    QPainter pp(&image);
    pp.drawPixmap(0,0,pixmap);
}


//鼠标点击
void MyWidget::mousePressEvent(QMouseEvent *ev)
{
    _lines.clear();
    _lines.append(ev->pos());
}


//鼠标移动
void MyWidget::mouseMoveEvent(QMouseEvent *ev)
{
    if(ev->buttons() & Qt::LeftButton )

        _lines.append(ev->pos());
        //强制重绘
    if((ev->pos().rx()<515)&&(ev->pos().ry()<515))
    emit go_thread(_lines);
}

//清空按钮
void MyWidget::on_clean_clicked()
{
    QPixmap cleanpix(500,500);
    cleanpix.fill(Qt::transparent);
    pixmap=cleanpix;
    update();
}

void MyWidget::getPixmap(QPixmap pix)
{
    pixmap=pix;
    update();
}

void MyWidget::kill_thread()
{
    child_thread->quit();
    child_thread->wait();
    delete child_object;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册