Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在Qt共享库项目中使用QSqlDatabase::drivers()时为空?_Qt_Shared Libraries_Ffi_Qtsql - Fatal编程技术网

为什么在Qt共享库项目中使用QSqlDatabase::drivers()时为空?

为什么在Qt共享库项目中使用QSqlDatabase::drivers()时为空?,qt,shared-libraries,ffi,qtsql,Qt,Shared Libraries,Ffi,Qtsql,我正在创建一个独立的Qt共享库。该库基本上充当执行SQLite查询的接口,因此需要使用QSqlDatabase模块。库的源代码: wxsqlite3接口.h: #ifndef WXSQLITE3INTERFACE_H #define WXSQLITE3INTERFACE_H #include <QtCore/qglobal.h> #if defined(WXSQLITE3INTERFACE_LIBRARY) # define WXSQLITE3INTERFACE_EXPORT

我正在创建一个独立的Qt共享库。该库基本上充当执行SQLite查询的接口,因此需要使用QSqlDatabase模块。库的源代码:

wxsqlite3接口.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
#ifndef WXSQLITE3INTERFACE#
#定义WXSQLITE3INTERFACE_H
#包括
#如果已定义(WXSQLITE3INTERFACE_库)
#定义WXSQLITE3INTERFACE_导出Q_DECL_导出
#否则
#定义WXSQLITE3INTERFACE_导出Q_DECL_导入
#恩迪夫
外部“C”WXSQLITE3INTERFACE_导出void init(const char*dbPath,const char*password);
外部“C”WXSQLITE3INTERFACE_导出常量字符*查询(常量字符*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);
}
#包括“wxsqlite3 interface.h”
#包括
#包括
#包括
#包括
#包括
#包括
void init(常量字符*数据库路径,常量字符*密码)
{

qDebug()如果我在这样构建之后重新构造目录,它会起作用:


因为您从未实例化正确运行Qt函数/类所需的Q(核心)应用程序。@chehrlic这意味着我不能在非Qt应用程序中使用使用Qt模块的库?@Kazuto_-Ute很可能,驱动程序需要QCoreApplication提供的信息(您不需要使用eventloop)@eyllanesc在调用QSqlDatabase::drivers()之前,我尝试在init函数中创建一个伪QCoreApplication实例,它消除了警告,但仍然返回空的driver listEnable环境变量
QT_DEBUG_PLUGINS=“1”
,以查看插件日志