SQLite与Qt-Quick的集成
我试图用QtQuick包含一个SQLite数据库,但我找不到任何示例。我只想能够访问数据库中的项目。有人知道我可以使用哪些示例程序吗?您可以举个例子,或者任何其他SQL模型,将其添加到QML上下文中,并在ListView中使用 创建模型 定义代理在QtQuick组件中按角色访问数据时将使用的角色名称(SQLite与Qt-Quick的集成,sqlite,qml,qt-quick,Sqlite,Qml,Qt Quick,我试图用QtQuick包含一个SQLite数据库,但我找不到任何示例。我只想能够访问数据库中的项目。有人知道我可以使用哪些示例程序吗?您可以举个例子,或者任何其他SQL模型,将其添加到QML上下文中,并在ListView中使用 创建模型 定义代理在QtQuick组件中按角色访问数据时将使用的角色名称(data方法有点幼稚,因为没有任何类型的错误检查): 将模型绑定到QML视图 将模型绑定到ListViewmodel属性,并使用以前定义的角色名称访问代理中的数据 ListView { id
data
方法有点幼稚,因为没有任何类型的错误检查):
将模型绑定到QML视图
将模型绑定到ListViewmodel
属性,并使用以前定义的角色名称访问代理中的数据
ListView {
id: someListView
model: someSqlModel
delegate: Text {
anchors.fill: parent
text: someRoleName
}
}
一些示例链接:
rebus的回答是一个很好的开始,但它让我想知道如何连接到现有的(SQLite)数据库 这里有一个完整的Qt5.x示例,不需要先决条件 请注意,该代码避免了SqlModel.data()中的角色名哈希表查找和所有多余的堆分配 main.cpp 资源。qrc
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
在Qt5.3中,有一个例子 示例/Qt-5.3/quick/controls/calendar 与rebus和Kay的答案相比,有一个有趣的注意事项:日历示例使用 qmlRegisterType()
这允许更具声明性的编程风格:不需要在main.cpp中实例化SqlModel。相反,您可以在qml中声明它 代码中没有任何“SqlQueryModel”实例。声明类SqlQueryModel有什么意义?@ondrejandrej这是一个输入错误,我已经修复了它,感谢您的注意。使用
QSqlQueryModel
使得定义SqlQueryModel
变得毫无意义,而且这个示例可能不会起作用,因为QML找不到someRoleName
角色。以正确的方式学习使用QML进行SQL集成是一个很好的切入点。很多感谢这是关于QML集成的一个好观点,但不幸的是,该示例中的模型的行为并不真正像一个模型,因为Calendar
已经有了自己的模型(一个月中的几天)。相反,它只获取特定日期的事件列表(一个QList
model)。最近它被简单地更改为一个QObject
,因为这样一个事实:我必须删除Q#u对象并包含“main.moc”。不知道这些是否重要,但除非我删除它们,否则无法构建。
ListView {
id: someListView
model: someSqlModel
delegate: Text {
anchors.fill: parent
text: someRoleName
}
}
#include <QtDebug>
#include <QString>
#include <QHash>
#include <QGuiApplication>
#include <QtQml>
#include <QQuickView>
#include <QtSql>
#include <QSqlQueryModel>
QSqlError initDb()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
// Open database file. The driver creates a new file if it doesn't exist yet.
db.setDatabaseName("mylibrary.sqlite");
if (!db.open())
return db.lastError();
QStringList tables = db.tables();
if (tables.contains("books", Qt::CaseInsensitive)) {
// DB has already been populated
return QSqlError();
}
QSqlQuery query;
if (!query.exec(QLatin1String("create table books(title varchar, author varchar)")))
return query.lastError();
if (!query.prepare(QLatin1String("insert into books(title, author) values(?, ?)")))
return query.lastError();
auto addBook = [&] (const QString& title, const QString& author) {
query.addBindValue(title);
query.addBindValue(author);
query.exec();
};
addBook("Mademoiselle de Maupin", "T. Gautier");
addBook("Der Nachsommer", "A. Stifter");
addBook("L'Education sentimentale", "G. Flaubert");
addBook("Voyna i mir", "L. Tolstoy");
addBook("Mysterier", "K. Hamsun");
addBook("The Sound and the Fury", "W. Faulkner");
addBook("Tender is the Night", "F. Scott Fitzgerald");
return QSqlError();
}
class SqlModel : public QSqlQueryModel
{
Q_OBJECT
public:
SqlModel(QObject* parent = 0)
: QSqlQueryModel(parent)
{
roleNamesHash.insert(Qt::UserRole, QByteArray("title"));
roleNamesHash.insert(Qt::UserRole + 1, QByteArray("author"));
}
QVariant data(const QModelIndex& index, int role) const
{
if(role < Qt::UserRole)
return QSqlQueryModel::data(index, role);
QSqlRecord r = record(index.row());
return r.value(role - Qt::UserRole);
}
QHash<int, QByteArray> roleNames() const { return roleNamesHash; }
private:
QHash<int, QByteArray> roleNamesHash;
};
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
auto err = initDb();
if (err.type() != QSqlError::NoError) {
qCritical() << err.text();
return 1;
}
SqlModel sqlModel;
sqlModel.setQuery("SELECT title, author FROM books");
QQuickView view;
QQmlContext *context = view.rootContext();
context->setContextProperty("sqlModel", &sqlModel);
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setSource(QUrl("qrc:///main.qml"));
view.show();
return app.exec();
}
#include "main.moc"
import QtQuick 2.1
Item {
width: 500
height: 300
ListView {
anchors { fill: parent; margins: 20 }
model: sqlModel
delegate: Text {
text: author + ' - ' + title
}
}
}
<RCC>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
CONFIG += c++11
QT += qml \
quick \
sql
SOURCES += main.cpp
OTHER_FILES += main.qml
RESOURCES += resources.qrc