qt - 在 qml 中创建一个表,它的自定义和与数据库的连接
问题描述
所以搜索后我决定使用 tableview 我的代码如下。
import QtQuick 2.15
import QtQuick.Controls 2.15
import Qt.labs.qmlmodels 1.0
Item {
width: 1126
height: 800
Rectangle {
id: rectangle
color: "#ffffff"
anchors.fill: parent
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
TableView {
anchors.fill: parent
columnSpacing: 1
rowSpacing: 1
clip: true
model: TableModel {
TableModelColumn { display: "customer" }
TableModelColumn { display: "address" }
TableModelColumn { display: "mobile" }
TableModelColumn { display: "email" }
TableModelColumn { display: "city" }
TableModelColumn { display: "state" }
TableModelColumn { display: "country" }
rows: [
{
"customer": "h",
"address": "down",
"mobile": "34556",
"email": "ty@gmail.com",
"city": "new york",
"state": "new york",
"country": "USA"
},
{
"customer": "h",
"address": "down",
"mobile": "34556",
"email": "ty@gmail.com",
"city": "new york",
"state": "new york",
"country": "USA"
},
{
"customer": "h",
"address": "down",
"mobile": "34556",
"email": "ty@gmail.com",
"city": "new york",
"state": "new york",
"country": "USA"
}
]
}
delegate: Rectangle {
implicitHeight: 50
implicitWidth: 100
border.width: 1
Text {
text: display
anchors.centerIn: parent
}
}
}
}
}
我怎么看不到列名?
解决方案
该TableModel
类型旨在支持非常简单的模型,而无需QAbstractTableModel
在 C++ 中创建自定义子类。
如果您需要在不使用 C++ 的情况下对表进行更多控制,最好使用另一种模型,例如ListModel
. 这里有一个例子:
import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Controls 1.4
import Qt.labs.qmlmodels 1.0
Item {
width: 1126
height: 800
Rectangle {
id: rectangle
color: "#ffffff"
anchors.fill: parent
anchors.rightMargin: 0
anchors.bottomMargin: 0
anchors.leftMargin: 0
anchors.topMargin: 0
TableView {
anchors.fill: parent
clip: true
TableViewColumn {
role: "customer"
title: "Customer"
}
TableViewColumn {
role: "address"
title: "Address"
}
TableViewColumn {
role: "mobile"
title: "Mobile"
}
model: ListModel {
ListElement {
customer: "h1"
address: "down1"
mobile: "345561"
}
ListElement {
customer: "h2"
address: "down2"
mobile: "345562"
}
ListElement {
customer: "h3"
address: "down3"
mobile: "345563"
}
}
}
}
}
推荐阅读
- javascript - 如何停止循环文本动画
- javascript - Typescript - 键入一个接受数组的函数,更改其属性之一,但仍返回相同的类型化数组
- c++ - 如何进行无分支数字循环?
- c++ - QGridLayout 小部件设置相等大小
- sql - 如果 count(column) 然后在 Oracle 中选择
- c# - 在 Cefsharp.WPF 中,我应该如何对“fi-FI”(芬兰语)语言进行拼写检查?
- python - 我在 Pygame 中收到一个关于字体的表面错误。我该如何解决?
- str-replace - 使用 str_replace 函数回显错误数据
- javascript - 获取“仅对页面组件执行导出的查询”。在 Gatsby 中尝试生成页面时
- flutter - 使用 Flutter 和 Dart 实现多个过滤器