【发布时间】:2021-08-18 10:05:36
【问题描述】:
我正在创建一个独立的 Qt 共享库。该库基本上充当执行 SQLite 查询的接口,因此它需要使用 QSqlDatabase 模块。库的源代码:
wxsqlite3-interface.h:
#ifndef WXSQLITE3INTERFACE_H
#define WXSQLITE3INTERFACE_H
#include <QtCore/qglobal.h>
#if defined(WXSQLITE3INTERFACE_LIBRARY)
# define WXSQLITE3INTERFACE_EXPORT Q_DECL_EXPORT
#else
# define WXSQLITE3INTERFACE_EXPORT Q_DECL_IMPORT
#endif
extern "C" WXSQLITE3INTERFACE_EXPORT void init(const char *dbPath, const char *password);
extern "C" WXSQLITE3INTERFACE_EXPORT const char *query(const char *strQuery);
#endif // WXSQLITE3INTERFACE_H
wxsqlite3-interface.cpp:
#include "wxsqlite3-interface.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
void init(const char *dbPath, const char *password)
{
qDebug() << "drivers" << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("SQLITECIPHER");
db.setDatabaseName(dbPath);
db.setPassword(password);
db.setConnectOptions("QSQLITE_USE_CIPHER=rc4");
}
const char *query(const char *strQuery)
{
QSqlQuery query(QSqlDatabase::database());
query.exec(strQuery);
QJsonArray result;
while (query.next()) {
const QSqlRecord rec = query.record();
QJsonArray jsonRec;
for (int i = 0; i < rec.count(); i++) {
jsonRec << QJsonValue::fromVariant(rec.value(i));
}
result << jsonRec;
}
return QJsonDocument(result).toJson(QJsonDocument::Compact);
}
构建库后,如果我在 Qt 应用程序中运行 init 函数,一切正常。但是,如果我使用 ffi-napi 在 node.js 应用程序中运行它,则不会加载任何驱动程序。我也收到警告:
QSqlDatabase:加载驱动插件需要一个QCoreApplication实例
构建库并运行windeployqt后我的目录是:
????wxsqlite3-interface
┣ ????sqldrivers
┃ ┣ ????qsqlite.dll
┃ ┣ ????qsqlodbc.dll
┃ ┣ ????qsqlpsql.dll
┃ ┗ ????sqlitecipher.dll
┣ ????Qt5Core.dll
┣ ????Qt5Sql.dll
┗ ????wxsqlite3-interface.dll
我是否缺少一些用于非 Qt 环境中的库的 dll?
【问题讨论】:
-
因为您从不实例化正确运行 Qt 函数/类所需的 Q(Core)Application。
-
@chehrlic 这意味着我不能在非 Qt 应用程序中使用使用 Qt 模块的库?
-
@Kazuto_Ute 很可能,驱动程序需要 QCoreApplication 提供的信息(您不需要使用事件循环)
-
@eyllanesc 我尝试在调用 QSqlDatabase::drivers() 之前在 init 函数中创建一个虚拟 QCoreApplication 实例,它摆脱了警告但仍然返回空驱动程序列表
-
启用环境变量
QT_DEBUG_PLUGINS="1"查看插件日志
标签: qt shared-libraries ffi qtsql