Qt中使用addLibraryPath添加路径后addLibraryPath没有更改的问题

23 阅读1分钟

本文只做演示

问题说明

当我使用addLibraryPath添加插件搜索路径, 随后使用libraryPaths进行打印时并没有修改内容

    QApplication a(argc, argv);
    
    QCoreApplication::addLibraryPath(QApplication::applicationDirPath() + "/lib");
    qDebug() << "Library paths:" << QCoreApplication::libraryPaths();
    
    MainWindow w;
    w.showMaximized();
    return a.exec();

解决方案

    QApplication a(argc, argv);
    QCoreApplication::setLibraryPaths(QCoreApplication::libraryPaths() << (QApplication::applicationDirPath() + "/lib"));
    qDebug() << "Library paths:" << QCoreApplication::libraryPaths();
    MainWindow w;
    w.showMaximized();
    return a.exec();