如何在使用QtNetwork和QThread的QT DLL中正确关闭QCoreApplication? [英] How can i properly close QCoreApplication in an QT DLL that is using QtNetwork and QThread?

查看:98
本文介绍了如何在使用QtNetwork和QThread的QT DLL中正确关闭QCoreApplication?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个QT DLL以在InnoSetup安装程序中使用它(InnoSetup用Delphi Pascal编写).

I'm trying to create a QT DLL to use it in an InnoSetup installer (InnoSetup is written in Delphi Pascal).

此DLL应该具有从InnoSetup调用时从互联网下载文件的功能.

This DLL should have a function to download a file from the internet when called from InnoSetup.

InnoSetup调用是这样的:

The InnoSetup call is made like this:

procedure downloadFile();
  external 'doDownload@files:testdll.dll,libssl-1_1.dll,libcrypto-1_1.dll stdcall loadwithalteredsearchpath delayload';

然后,我用这个来称呼它:

Then, I call it using this:

function InitializeSetup(): Boolean;
begin
  Result := True;
  ExtractTemporaryFile('testdll.dll');
  downloadFile();
end;

问题是,如果我需要稍后(在首次调用之后)调用它,我将无法重用 downloadFile(); ,因为在第一次调用时, 我怀疑 ,QCoreApplication无法正确关闭,并保持在exec循环中.当我第二次调用此函数时,什么也没有发生,也没有文件从互联网上下载.再次下载文件的唯一方法是关闭并重新打开Inno Setup.

Problem is, i can't reuse downloadFile(); if i need to call it later(after initial call) because on first call, somehow, i suspect, QCoreApplication doesn't properly close and remains in an exec loop. When i call this function a second time, nothing happens, no file is downloading from the internet. The only way to download a file again is to close and reopen Inno Setup.

这是我的代码:

TESTDLL.pro

QT -= gui
QT += network

TEMPLATE = lib
DEFINES += TESTCLASS_LIBRARY

CONFIG += c++11 dll

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    testdll.cpp \
    libs\downloadThread.cpp

HEADERS += \
    testdll_global.h \
    testdll.h \
    libs\downloadThread.h

QMAKE_LFLAGS += -Wl,--output-def,testdll.def

# Default rules for deployment.
unix {
    target.path = /usr/lib
}
!isEmpty(target.path): INSTALLS += target

TESTDLL.h

#ifndef TESTDLL_H
#define TESTDLL_H

#include <QtCore>

#include "testdll_global.h"
#include "libs/downloadThread.h"


class TestDLL : public QObject
{
    Q_OBJECT
public slots:
    void handleResults(const QString &);
    void startWThread();
    void initQCoreApp();

private:
    void debugMsg(QString fileName, QString message)
    {
        QFile file(fileName);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << message;
        file.close();
    };
};

#endif

TESTDLL.cpp

#include "testdll.h"

namespace QCoreAppDLL
{
    static int argc = 1;
    static char arg0[] = "testdll.cpp";
    static char * argv[] = { arg0, nullptr };
    static QCoreApplication * pApp = nullptr;
}

extern "C" TESTCLASS_EXPORT void doDownload()
{
    TestDLL a;
    a.initQCoreApp();
}

void TestDLL::startWThread()
{
    downloadThread *thread = new downloadThread(this);
    connect(thread, &downloadThread::finished, thread, &QObject::deleteLater);
    connect(thread, &downloadThread::resultReady, this, &TestDLL::handleResults);
    thread->start();
}

void TestDLL::initQCoreApp()
{

    if (!QCoreApplication::instance())
    {
        QCoreAppDLL::pApp = new QCoreApplication(QCoreAppDLL::argc, QCoreAppDLL::argv);

        TestDLL a;
        a.startWThread();

        QCoreAppDLL::pApp->exec();
    }
}

void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp)
        QCoreAppDLL::pApp->quit();
}

DOWNLOADTHREAD.h

#ifndef downloadThread_H
#define downloadThread_H

#include <QtNetwork>
#include <QDebug>

class downloadThread : public QThread
{
    Q_OBJECT
signals:
    void resultReady(const QString &s);
public:
    downloadThread(QObject *parent);
    ~downloadThread();

    void run() override {
        QString result;

        initDownload();

        emit resultReady(result);
    };
    void initDownload();
    void doDownload(QString url);
private:
    QNetworkAccessManager *networkMgr;
    QNetworkReply *_reply;
public slots:
  void replyFinished(QNetworkReply * reply);
};
#endif

DOWNLOADTHREAD.cpp

#include "downloadThread.h"

downloadThread::downloadThread(QObject *parent)
    : QThread(parent)
{
}

downloadThread::~downloadThread()
{
   quit();
   wait();
}

void downloadThread::initDownload()
{
    doDownload("http://www.google.com");
    exec();
}

void downloadThread::doDownload(QString url)
{
    networkMgr = new QNetworkAccessManager;

    QNetworkRequest request;
    request.setUrl(QUrl(url));
    networkMgr->get(request);

    connect(networkMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}

void downloadThread::replyFinished(QNetworkReply * reply)
{
    if(reply->error() == QNetworkReply::NoError)
    {
        QByteArray content = reply->readAll();
        QSaveFile file("output.txt");
        file.open(QIODevice::WriteOnly);
        file.write(content);
        file.commit();

        reply->deleteLater();
        this->exit();
    } else {
        //error
        reply->deleteLater();
        this->exit();
    }
}

我自己尝试解决的问题:

  • 使用删除QCoreAppDLL :: pApp
void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp){
        QCoreAppDLL::pApp->quit();
        delete QCoreAppDLL::pApp;
    }
}

使主机应用崩溃.

  • 使用 QTimer SLOT(quit())
void TestDLL::initQCoreApp()
{

    if (!QCoreApplication::instance())
    {
        QCoreAppDLL::pApp = new QCoreApplication(QCoreAppDLL::argc, QCoreAppDLL::argv);

        TestDLL a;

        connect(&a, SIGNAL(finished()), QCoreAppDLL::pApp, SLOT(quit()));
        QTimer::singleShot(0, &a, SLOT(startWThread));

        QCoreAppDLL::pApp->exec();
    }
}

什么也没发生,就像QCoreApplication exec循环在QThread完成工作之前结束.

nothing happens, it's like QCoreApplication exec loop ends before QThread finish the job.

  • 在QThread对象上使用插槽/信号
void TestDLL::startWThread()
{
    downloadThread *thread = new downloadThread(this);
    connect(thread, &downloadThread::finished, thread, &QObject::deleteLater);
    connect(thread, &downloadThread::resultReady, this, &TestDLL::handleResults);
    connect(thread, SIGNAL(finished()), qApp, SLOT(quit()));
    thread->start();
}

具有与以下行为相同的行为:

that has the same behavior as:

void TestDLL::handleResults(const QString &)
{
    debugMsg("result.txt","succes!");

    if (QCoreAppDLL::pApp)
        QCoreAppDLL::pApp->quit();
}

我没主意了,我还能做什么来解决这个问题?

I'm out of ideas, what else can i do to fix this?

先谢谢大家!

推荐答案

我终于设法自己解决了这个难题.这是给那些有兴趣的人的.

I've finally managed to solve the dillema by myself. Here it is for those that are interested.

我创建了一个新功能:

void TestDLL::deinitQCoreApp()
{
    if (QCoreAppDLL::pApp)
        delete qApp;
}

现在,我的外部功能变为:

and now, my external function becomes:

extern "C" TESTCLASS_EXPORT void doDownload()
{
    TestDLL a;
    a.initQCoreApp();

    TestDLL b;
    b.deinitQCoreApp();
}

当然, TESTDLL.h 有一个新条目:

#ifndef TESTDLL_H
#define TESTDLL_H

#include <QtCore>

#include "testdll_global.h"
#include "libs/downloadThread.h"


class TestDLL : public QObject
{
    Q_OBJECT
public slots:
    void handleResults(const QString &);
    void startWThread();
    void initQCoreApp();
    void deinitQCoreApp();

private:
    void debugMsg(QString fileName, QString message)
    {
        QFile file(fileName);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream out(&file);
        out << message;
        file.close();
    };
};

#endif

现在,我可以在主机上重用该功能多少次了.问题已解决!

Now, i can reuse the function how many times i want on the host. Problem solved!

这篇关于如何在使用QtNetwork和QThread的QT DLL中正确关闭QCoreApplication?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆