【发布时间】:2014-08-04 18:38:19
【问题描述】:
我必须在 Qt 和 QML 之间进行集成,我在 QML 中设计了一个带有 TableView 的简单窗口,我想用 C++ 代码填充它,但我不知道如何做到这一点,我只用一周的时间使用 Qt。 这是我的 QML 窗口代码,但我需要帮助才能知道如何填充它。此外,我想知道如何在 Qt 的 Qml 屏幕中打印信息,例如进程状态,并使其自动滚动。我认为这个想法大致相同,如何在 QML 对象中打印信息。 谢谢
import QtQuick 2.2
import QtQuick.Window 2.1
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.0
import QtQuick.Dialogs 1.0
Window {
id: winparent
visible: true
color: "gray"
width: 700
height: 500
Rectangle{
color: "lightblue"
ToolBar {
id: toolbar
width: winparent.width
height: 120
Text {
id: title
x: 20
y: 20
text: "Book Searching"
color: "darkblue"
font.pixelSize: 30
// font{family: 'Courier'; pixelSize: 20; bold: true; capitalization: Font.SmallCaps;}
} // title
CheckBox {
id: enabledCheck
text: "Enabled"
checked: true
anchors.right: parent.right
y: 80
// anchors.verticalCenter: parent.verticalCenter
}
}
ListModel {
id: libraryModel
// ListElement{ title: "A Masterpiece" ; author: "Gabriel" }
// ListElement{ title: "Brilliance" ; author: "Jens" }
// ListElement{ title: "Outstanding" ; author: "Frederik" }
} // libraryModel
TableView {
x: 20
y: 170
width: 660
height: 200
enabled: enabledCheck.checked
TableViewColumn{ role: "title" ; title: "Title" ; width: 150 ;resizable: false ; movable: false }
TableViewColumn{ role: "author" ; title: "Author" ; width: 150 ;resizable: false ; movable: false }
TableViewColumn{ role: "year" ; title: "Year" ; width: 150 ;resizable: false ; movable: false }
TableViewColumn{ role: "revision" ; title: "Revision" ; width: 200 ; resizable: false ; movable: false }
model: ListModel{
id: tV
}
alternatingRowColors: true
backgroundVisible: true
headerVisible: true
itemDelegate: Item {
Text {
anchors.verticalCenter: parent.verticalCenter
// color: "blue"
// if (enabledCheck.checked = false)
// color: "gray"
enabled: enabledCheck.checked
elide: styleData.elideMode
text: styleData.value
} // text
} // Item
}// TableView
Button {
text: "Add row"
x:20
y:130
onClicked: {
tV.append({title: "some value",
author: "Another value",
year: "One more value",
revision: "uno mas"});
}
}
focus: true
Keys.onPressed: {
if (event.key == Qt.Key_Escape) {
Qt.quit();
event.accepted = true;
} //if
if (event.key == Qt.Key_C) {
console.log(c2);
}
} //Keys.onPressed
} // Rectangle
}
【问题讨论】:
-
“在 Qt 的 Qml 屏幕中打印信息”是什么意思?您可以通过将数据附加到模型来将数据添加到表中:
libraryModel.append({title: "xxx", author: "yyy"}) -
by "print info in qml" 是因为我要从qt做一些查询,把信息保存在模型中并在qml中显示,thnx