天天看點

Qt WebEngine無法啟動

标題比較含糊,具展現象是在dll中加入webenginewidgets子產品,并通過QWebEngineView打開網頁。然後在main函數中通過QLibrary的load方法加載該dll,但是load既不傳回true也不傳回false,從任務管理器可以看出main函數所在程序已經起來了,也就是說load方法阻塞了。

解決方法:如果要在dll或者plugin中使用webenginewidgets子產品,必須在main函數QApplication或QCoreApplication建立之前添加Qt::AA_ShareOpenGLContexts屬性。如下所示:

#include <QLibrary>
#include <QDebug>
#include <QApplication>
typedef void (*LoadFunction)();
#define DLLPATH "testui.dll"
#define DLLPORC "loadWnd"
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
    QApplication a(argc, argv);
    QLibrary testuilib(DLLPATH);

    if (testuilib.load())
    {
        LoadFunction pLoad = (LoadFunction)testuilib.resolve(DLLPORC);
        if (pLoad)
        {
           pLoad();

        }
    }
    else
    {
        qDebug("testui.dll load failed");
    }
    return a.exec();
}
           
Qt WebEngine無法啟動

繼續閱讀