根据我的理解,要在QML中将属性设为数组,您必须将其指定为类型variant或var:
property var myArray:[]
这看起来与以下内容完全相同:
property variant myArray:[]
这是真的?
我有一部分HTML代码,显示小表.在浏览器中,它看起来像在图片中:
但是,当我想在Text QML(根据文档,应该支持HTML)中显示它时,我看到:

(橙色矩形是矩形的一部分,是父的mytext)
Text {
id: mytext
anchors.fill: parent
text: "<div><table border='1'><caption><h4>Test stats</h4>"+
"</caption><tr bgcolor='#9acd32'><th/><th>Number1</th><th>Number2</th></tr> <tr><th>Line1</th>"+
"<td> 0 </td> <td> 1 </td> </tr> <tr><th>Line2</th> <td> 0 </td> <td> 1 </td> </tr>"+
"<tr><th>Line3</th> <td> 0 </td> <td> 0 </td> </tr> <tr><th>Line4</th> <td> 1 </td> <td> 0 </td> </tr>"+
"<tr><th>Line5</th> <td> 1 </td> <td> 1 </td> </tr> <tr><th>Line6</th> <td> 1 </td> <td> 1 </td> </tr> </div>"
}
Run Code Online (Sandbox Code Playgroud)
那么如何在QML(QtQuick 2.0)中正确显示这个HTML表格呢?是否可以不使用WebView?
如何在QML中声明list属性(Qt.labs.settings在我的情况下使用):
Settings {
property list recentFiles: []
}
Run Code Online (Sandbox Code Playgroud)
不行.我尝试过许多其他选项:list<string>,string[],等无似乎工作.
我一直在尝试使用QML TableView来显示QAbstractTableModel.等式中缺少的部分似乎是不可能有一个可变数量的列TableView,尽管覆盖QAbstractItemModel::roleNames哪个应该告诉Qt我的列的数量和名称.我尝试仅使用QML进行测试:
import QtQuick 2.0
import QtQuick.Controls 1.1
Rectangle {
anchors.fill: parent
property real showImage: 1.0
width: 500
TableView {
id: myTable
model: myModel
// TableViewColumn {
// role: "title"; title: "Name"; width: 200
// }
}
ListModel {
id: myModel
ListElement {
title: "item one"
}
ListElement {
title: "item two"
}
}
}
Run Code Online (Sandbox Code Playgroud)
在运行时,尽管TableView包含ListElement在其中定义了角色的s的模式,但这并未显示任何内容.
但是,如果上面的代码被取消注释并且TableViewColumn定义了a ,则该列将按预期显示该角色的数据,但该表仍将不显示任何其他角色.显然,这只适用于静态定义的列数,而不适用于直到运行时才知道列数的情况.
给出的示例与我的实际示例基本相同,只是我的模型是用C++定义的.
编辑:我曾尝试调用javascript函数:
function …Run Code Online (Sandbox Code Playgroud) 如何在QtQuick Controls 2中安装菜单栏?它曾经是这样的(在ApplicationWindow中):
menuBar: MenuBar {
Menu {
title: qsTr('File')
MenuItem {
text: qsTr('&Test')
onTriggered: console.log('test')
}
MenuItem {
text: qsTr('&Exit')
onTriggered: Qt.quit();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但升级到Qt 5.7后,它会出现此错误: Invalid property name "menuBar".(M16)
PS它曾经使用设备的本机菜单系统,例如在OS X上它使用原生屏幕的topbar菜单栏,在Linux和Windows上它在应用程序topbar菜单栏中使用原生菜单等.
我可能需要读取或写入一些的性能Loader的sourceComponent一些外部的功能.
访问x这个对象内部属性的方法Loader是sourceComponent什么?
import QtQuick 2.0
Item {
width: 200; height: 200
Loader {
anchors.fill: parent
sourceComponent: rect
}
Component {
id: rect
Rectangle
{
width: 50
height: 50
color: "red"
property int x
}
}
}
Run Code Online (Sandbox Code Playgroud) 我需要做的是:我需要使用存储聊天消息的QML 创建聊天窗口ListView.我设置listView.positionViewAtEnd()为了跟随最后的消息.positionViewAtEnd当我向上滚动时,我禁用,这样我每次收到新消息时都可以读取过去的消息,而不会跳到最后.
问题:向上滚动后,每次收到新消息时,它都会跳到列表的开头.为了解决这个问题,我设法存储contentY列表并在每次onCountChanged调用处理程序时重置它(请参阅下面的代码):
ListView {
id: messagesList
model: contact? contact.messages: []
delegate: delegate
anchors.fill: parent
anchors.bottomMargin: 20
height: parent.height
anchors.margins: 10
property int currentContentY
onMovementEnded: {
currentContentY = contentY
}
onCountChanged: {
contentY = currentContentY
}
onContentYChanged: {
console.log(".....contentY: " + contentY)
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,即使我设置了最后一个contentY,在模型更改之前,列表仍然会跳转一些(几个像素,而不是结束或开始)并且它不会始终跳转.当我走到列表的顶部并打印出来时,contentY我会得到负值.理论上,contentY在列表的开头应该是0.
有人能告诉我出了什么问题吗?或者可能建议另一种解决方案来创建我的消息列表
比你提前!:)
我曾多次遇到这个问题,动态创建对象,无论它们是用QML还是C++创建的.在仍在使用时删除对象,导致硬崩溃,没有明显原因.这些对象仍然被引用并且是其他对象的一部分,一直到根对象,所以我发现QML在它们的引用数仍然高于零时删除这些对象是很奇怪的.
到目前为止,我找到的唯一解决方案是使用C++创建对象并将所有权明确设置为CPP,从而无法从QML中删除对象.
起初我认为它可能是一个父母问题,因为我使用QObject派生类,动态实例化的QML方法传递Item一个父类,而QtObject甚至没有父属性 - 它没有公开QObject.
但后来我尝试了一个Qobject派生的暴露和使用育儿,最后甚至尝试使用Item只是为了确保对象是正确的父对象,但这种行为仍然存在.
这是一个产生这种行为的例子,遗憾的是我无法将其压平为单个源,因为Components 的深层嵌套会破坏它:
// ObjMain.qml
Item {
property ListModel list : ListModel { }
Component.onCompleted: console.log("created " + this + " with parent " + parent)
Component.onDestruction: console.log("deleted " + this)
}
// Uimain.qml
Item {
id: main
width: childrenRect.width
height: childrenRect.height
property Item object
property bool expanded : true
Loader {
id: li
x: 50
y: 50
active: expanded && …Run Code Online (Sandbox Code Playgroud) 我有一个使用Open GL绘制的Qt应用程序。在某些时候,我正在使用与QQuickWindow关联的QQuickRenderControl方法将QML场景绘制到纹理中,以便稍后在最终图像中进行合成。
现在,我正在考虑将OpenGL移植到Vulkan,但不确定在此QML层是否可以这样做。
阅读Qt文档,我发现
QQuickWindow使用OpenGL顶部的场景图进行渲染。
您认为可以将其移植到Vulkan吗?也许是压倒一切的QQuickWindow和QQuickRenderControl?我不是Qt专家,所以也许有人可以让我对问题有更好的了解。