qt creator部署sqlite

qt creator deplooy sqlite

我正在尝试使用 QT 和 SQLite 数据库部署 windows 应用程序。我在没有 QT 的情况下拥有它 运行,但是如何在不使用我的计算机的文件路径的情况下包含数据库。我想将它部署到其他系统上。下面是我从数据库名称使用的路径示例。

db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("C:\Users\johnm\Documents\build-Login-Desktop_Qt_5_10_1_MinGW_32bit-Release\release\GbManuf.db");

在此先感谢您的帮助。

一个可能的解决方案是使用 QCoreApplication::applicationDirPath() returns 可执行文件所在的目录

QString path = QDir(QCoreApplication::applicationDirPath()).filePath("GbManuf.db");
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(path);

对于各种可访问的公共路径,可以使用QStandardPaths
例如:

QString homeDirectory = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

有许多 QStandardPath 可供查找:AppDataLocation、CacheLocation、DownloadLocation 等。

存储应用程序数据QStandardPaths::AppDataLocation可以使用。 From Qt doc;

Returns a directory location where persistent application data can be stored. This is an application-specific directory. To obtain a path to store data to be shared with other applications, use QStandardPaths::GenericDataLocation. The returned path is never empty. On the Windows operating system, this returns the roaming path. This enum value was added in Qt 5.4.