QThread: Destroyed while thread is still running

186
05 августа 2018, 06:50

Что я не так делаю ? Передаю две ссылки на файл в ThreadsCopy :

void MainWindow::SaveFiles()
{
    if (!ComboBoxLog->currentData().isNull()) {
        SaveFilesButton->setDisabled(true);
        Progress->show();
        Progress->setMinimum(0);
        Progress->setMaximum(model->rowCount()-1);
        Login conn;
        conn.connOpen();
        QSqlQuery* queryAddNote = new QSqlQuery(conn.mydb);
        QString newDir = "";
        for (int i=1;i<model->rowCount();i++) {
            newDir = QString(QDir::currentPath()+"/"+ComboBoxLog->currentText()+"_%1"+"/"+model->index(i,2).data().toString()).arg(QString(QDate::currentDate().toString()).replace(" ","_"));
            if (QDir(newDir).exists() == false) {
                QDir().mkpath(newDir);
            }
            if (model->index(i,0).data(Qt::DisplayRole).toBool()==true) {
                QString FileType = MainWindow::getFileType(QFileInfo(model->index(i,1).data().toString()).fileName());
                if (FileType == "Рисунок JPEG") {
                    QString pathFile = model->index(i,1).data().toString();
                    QByteArray pathFileBA = pathFile.toLocal8Bit();
                    const char *pathFileChar = pathFileBA.data();
                    // Read the JPEG file into a buffer
                    FILE *fp = fopen(pathFileChar, "rb");
                    if (!fp) {
                        msgBox.setText("Не удается открыть файл.\n");
                        msgBox.exec();
                        qDebug()<<"Не удается открыть файл.\n"<<pathFileChar;
                    }
                    fseek(fp, 0, SEEK_END);
                    unsigned long fsize = ftell(fp);
                    rewind(fp);
                    unsigned char *buf = new unsigned char[fsize];
                    if (fread(buf, 1, fsize, fp) != fsize) {
                        msgBox.setText("Не удается прочитать файл.\n");
                        msgBox.exec();
                        delete[] buf;
                    }
                    fclose(fp);
                    // Parse EXIF
                    easyexif::EXIFInfo result;
                    int code = result.parseFrom(buf, fsize);
                    delete[] buf;
                    if (code) {
                        msgBox.setText(QString("Ошибка анализа EXIF: код %1 \n").arg(code));
                        msgBox.exec();
                    }
                    ThreadsCopy threadA("thread A",model->index(i,1).data().toString(),newDir+"/"+QFileInfo(model->index(i,1).data().toString()).fileName());
                    threadA.start();
                    QString sqlQueryAddNote = QString("INSERT INTO \"SchemeObservationLog\".data (data,path_to_data,id_log,latitude,longitude,date_create,date_add_log) VALUES ('%1','%2','%3','%4','%5','%6','%7')")
                            .arg(QFileInfo(model->index(i,1).data().toString()).fileName())
                            .arg(newDir+"/"+QFileInfo(model->index(i,1).data().toString()).fileName())
                            .arg(ComboBoxLog->currentData().toString())
                            .arg(QString::number(result.GeoLocation.Latitude))
                            .arg(QString::number(result.GeoLocation.Longitude))
                            .arg(result.DateTimeOriginal.c_str())
                            .arg(QDate::currentDate().toString());
                    queryAddNote->prepare(sqlQueryAddNote);
                    queryAddNote->exec();
                } else {
                    ThreadsCopy threadB("thread B",model->index(i,1).data().toString(),newDir+"/"+QFileInfo(model->index(i,1).data().toString()).fileName());
                    threadB.start();
                    QString sqlQueryAddNote = QString("INSERT INTO \"SchemeObservationLog\".data (data,path_to_data,id_log,date_add_log) VALUES ('%1','%2','%3','%4')")
                            .arg(QFileInfo(model->index(i,1).data().toString()).fileName())
                            .arg(newDir+"/"+QFileInfo(model->index(i,1).data().toString()).fileName())
                            .arg(ComboBoxLog->currentData().toString())
                            .arg(QDate::currentDate().toString());
                    queryAddNote->prepare(sqlQueryAddNote);
                    queryAddNote->exec();
                }
                Progress->setValue(i);
            } else {
            }
        }
        conn.connClose();
        Progress->hide();
        model->clear();
        SaveFilesButton->setDisabled(false);
        TableDisk->setModel(nullptr);        
        MainWindow::TableLogBuilding("SELECT * FROM \"SchemeObservationLog\".data");
        MainTab->setCurrentIndex(1);
    } else {
        msgBox.setText("Выберите журнал в ComboBox! Обязательное поле.");
        msgBox.exec();
        return;
    }
    return;
}

threadscopy.h

#ifndef THREADSCOPY_H
#define THREADSCOPY_H
#include <QThread>
#include <login.h>
#include <QFile>
class ThreadsCopy : public QThread
{
public:
    explicit ThreadsCopy(QString threadName, QString fromPath, QString toPath);
    // Переопределяем метод run(), в котором будет
    // располагаться выполняемый код
    void run();
    QString _fromPath;
    QString _toPath;
    QMessageBox msgBox;
private:
    QString name;   // Имя потока
};
#endif // THREADSCOPY_H

threadscopy.cpp

#include "threadscopy.h"
#include <QDebug>
ThreadsCopy::ThreadsCopy(QString threadName, QString fromPath, QString toPath) :
    name(threadName),
    _fromPath(fromPath),
    _toPath(toPath)
{
}
void ThreadsCopy::run()
{
    qDebug()<<name;
    qDebug()<<_fromPath;
    qDebug()<<_toPath;
    QFile::copy(_fromPath,_toPath);
}
READ ALSO
Не работает codeblocks на linux ubuntu

Не работает codeblocks на linux ubuntu

Я только недавно решил установить себе впервые линуксПроблема в следующем, при сборке выдает вот это :

183
Параметризовать компаратор

Параметризовать компаратор

Вот такую задачку "нарезали", не знаю с какой стороны подойтиМожет кто поможет

207