- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在 qml TableView 中显示 QSqlQueryModel,但我不想为每个新查询创建单独的 QML 文件,因为我无法创建给定的无限 qml 文件 here .还有 question对于动态列数不适合我(可能是版本差异,因为我使用的是 5.11)。我只想要类似的东西:-
QTableView *view = new QTableView;
view->setModel(model);
view->show();
在 QML 中。
我是 qml 的新手。到目前为止,我能够按照第一个链接中的指导显示 QSqlQueryModel,但我的用户可以输入任何 SQL 查询。
最佳答案
使用来自 A more generic approach 的信息 How to Use a QSqlQueryModel in QML您可以构建一个通用模型,以便从 QML 轻松使用您可以创建一个属性来传递查询。
sqlquerymodel.h
#ifndef SQLQUERYMODEL_H
#define SQLQUERYMODEL_H
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlRecord>
class SqlQueryModel : public QSqlQueryModel
{
Q_OBJECT
Q_PROPERTY(QString query READ queryStr WRITE setQueryStr NOTIFY queryStrChanged)
Q_PROPERTY(QStringList userRoleNames READ userRoleNames CONSTANT)
public:
using QSqlQueryModel::QSqlQueryModel;
QHash<int, QByteArray> roleNames() const
{
QHash<int, QByteArray> roles;
for (int i = 0; i < record().count(); i ++) {
roles.insert(Qt::UserRole + i + 1, record().fieldName(i).toUtf8());
}
return roles;
}
QVariant data(const QModelIndex &index, int role) const
{
QVariant value;
if (index.isValid()) {
if (role < Qt::UserRole) {
value = QSqlQueryModel::data(index, role);
} else {
int columnIdx = role - Qt::UserRole - 1;
QModelIndex modelIndex = this->index(index.row(), columnIdx);
value = QSqlQueryModel::data(modelIndex, Qt::DisplayRole);
}
}
return value;
}
QString queryStr() const{
return query().lastQuery();
}
void setQueryStr(const QString &query){
if(queryStr() == query)
return;
setQuery(query);
emit queryStrChanged();
}
QStringList userRoleNames() const {
QStringList names;
for (int i = 0; i < record().count(); i ++) {
names << record().fieldName(i).toUtf8();
}
return names;
}
signals:
void queryStrChanged();
};
#endif // SQLQUERYMODEL_H
main.cpp
#include "sqlquerymodel.h"
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QDebug>
#include <QSqlError>
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
qDebug()<<"Cannot open database\n"
"Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit.";
return false;
}
QSqlQuery query;
if(!query.exec("CREATE TABLE COMPANY("
"ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
"NAME TEXT NOT NULL,"
"AGE INT NOT NULL,"
"SALARY REAL"
")")){
qDebug()<<query.lastError().text();
}
for(int i=0; i < 10; i++){
query.prepare("insert into COMPANY(NAME, AGE, SALARY) values(:name, :age, :salary)");
query.bindValue(":name", QString("name-%1").arg(i));
query.bindValue(":age", (i+1)*1000);
query.bindValue(":salary", (11-i)*11.5);
if(!query.exec()){
qDebug()<<query.lastError().text();
}
}
return true;
}
int main(int argc, char *argv[])
{
qmlRegisterType<SqlQueryModel>("Foo", 1, 0, "SqlQueryModel");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
if(!createConnection())
return -1;
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
main.qml
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import Foo 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("SqlQueryModel")
SqlQueryModel{
id: sqlmodel
query: "select * from COMPANY"
}
Component{
id: columnComponent
TableViewColumn{width: 100 }
}
TableView {
id: view
anchors.fill: parent
resources:{
var roleList = sqlmodel.userRoleNames
var temp = []
for(var i in roleList){
var role = roleList[i]
temp.push(columnComponent.createObject(view, { "role": role, "title": role}))
}
return temp
}
model: sqlmodel
}
}
关于c++ - 如何在 QML 中显示 QSqlQueryModel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52468892/
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如果下一个元素的宽度超过指定布局的宽度,是否有 QML 布局或某些配置会自动将 QML 项目包装到下一行? 当我使用 QML GridLayout ,项目刚好离开窗口的边缘并被剪裁: GridLayo
如何在 qml 文件之间发送变量或信号? http://i.stack.imgur.com/MChCG.png Mainwindow -> 创建组件Item2.qml MainWindow -> 创建
我正在做一些事情,我有一个名为“FloatingMenu”的类(它应该在 C++ 中管理菜单)及其在文件 FloatingMenu.qml 中用于 GUI 的 QML alter-ego。我有一个文件
我正在尝试做一些看似简单的事情,但失败了:定义一个简单的内联文本格式组件,然后用不同的文本多次实例化它。这是代码 Item { . . . Component { id: favButtonL
我可以在页面中使用 InvokeActionItem 轻松共享项目,但我需要能够在 ListView 项目中调用它。我设法触发了一个调用,但我不知道如何在触发它时添加数据。我不断收到错误消息 Invo
我如何在 QML 中检测 Window {} 之外的点击? Rectangle { id: topLevel height: 400; width: 400 Window {
我试过 : var child = grid.children[slot1]; grid.children[slot1] = grid.children[slot2]; grid.children[s
例如,我希望创建一个包含 100 个文本编辑器的 qml 窗口,如何在循环中创建它?那可能吗? 最佳答案 循环是命令式代码,所以它不是 QML,而是 Javascript 或 C++。所以当然,你可以
这是我的 QML 文件,其中包含一个文本组件: import QtQuick 2.0 Item { id: idItmWrapText Text { id: idTxt
我正在寻找一种方法来显示一个文本提示,说明预期的输入作为对用户的建议。以谷歌搜索栏为例: 是否有我缺少的属性,或者这是必须通过脚本来实现的? 最佳答案 Qt Quick 输入项上不存在该属性。您可以为
为 qml 项设置背景的最简单方法是让子矩形的 anchor 完全填满父项: Item { width: 320 height: 240 Rectangle {
我想将属性动态添加到 QML 元素: Item { id: dynamicProperty; property int first; Component.onCompleted: { /*
我用 PySide 和 QML 编写了某种安装程序。按照设计,它必须是多页的。而且我想将要从 QML 表单调用的插槽划分为不同的对象(理想情况下,划分为模块,但据我了解,带有插槽的对象必须继承 QOb
QML 中有没有办法用 opacity: 0 创建一个矩形?仍然有可见的边界?如果没有,有关如何解决它的任何建议? 谢谢 最佳答案 不,不透明度适用于项目的完整视觉方面(并且不透明度:0 使项目完全不
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我在 SO 上查看了大量关于 QML 内容边距的问题,但所有答案都指向缺少 spacing: 0 属性。我已经完成了所有这些,但仍然有一些我无法消除的奇怪空间。任何人都可以解释为什么这个 QML 代码
我有一个用于样式定义的 QML 单例,定义如下: pragma Singleton import QtQuick 2.2 import QtQuick.Controls 1.1 import QtQu
这是以下代码的结果: 主.qml import QtQuick 2.8 Item { Reusable { index: 1234 // reusable with
属性变体 a:{}似乎不起作用。 a 最终未定义,而不是一个空字典。 我对 Javascript 不是很有经验...初始化属性以保存空字典的正确方法是什么? 以下 qml 在控制台上打印“qrc:/m
我是一名优秀的程序员,十分优秀!