我必须在Qt和QML之间进行集成,我在QML中设计了一个带有TableView的简单窗口,我想用C ++代码填充它,但我不知道如何做到这一点,我和Qt一起工作一周。 这是我的QML窗口代码,但我需要帮助才能知道如何填充它。此外,我想知道如何在Qt屏幕上打印Qt信息,如进程状态,并使其自动滚动。我认为这个想法大致相同,如何在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
}
答案 0 :(得分:0)
您应该在C ++中创建一个模型,例如,通过继承QAbstractTableModel
并使用它设置TableView
的模型属性。要在qml中使用它,首先需要使用setContextProperty
使qml知道它。例如:
MySubClass my_model;
engine->rootContext()->setContextProperty("model_", &my_model);
其中engine
是您使用的qml引擎的实例。