これが私が今持っているものですTableView
:
TableView{
model: testContext.list
TableViewColumn{
width: parent.width / 2
title: "Name"
delegate: Text {
text: modelData.name
}
}
TableViewColumn{
width: parent.width / 2
title: "Age"
delegate: Text {
text: modelData.age
}
}
Keys.onDeletePressed: testContext.removeItem(currentRow)
onClicked: {
console.log(row)
}
Menu {
id: contextMenu
MenuItem {
text: "Delete"
onClicked: testContext.removeItem(currentRow)
}
}
}
アイテムを選択してを押すとDelete、選択したアイテムが削除されますがcontextMenu
、右クリックして選択したアイテムを削除することをまだ表示できていません。testContext.list
はでQVector<QObject*>
あり、ListView/GridView
それで私はこのようなものを持つことができます:
ListView/GridView{
id: listView
anchors.fill: parent
model: testContext.list
//cellHeight:
//cellWidth:
delegate: Text{
text: modelData.name + " " + modelData.age
MouseArea {
acceptedButtons: Qt.LeftButton | Qt.RightButton
anchors.fill: parent
onClicked: {
listView.currentIndex = index
listView.forceActiveFocus()
if (mouse.button == Qt.RightButton)
contextMenu.popup()
}
}
}
Menu {
id: contextMenu
MenuItem {
text: "Delete"
onClicked: testContext.removeItem(listView.currentIndex)
}
}
Keys.onDeletePressed: testContext.removeItem(listView.currentIndex)
highlight: Rectangle {
color: "lightgray"
width: listView.width
}
}
を押すDeleteか使用してアイテムを削除しますcontextMenu
。
私は、に加えて、MouseArea
他のいくつかにをsignal-handler
追加して試しましたが、これまでのところ運がありません!onClicked
TableView
次の例は、コンテキストメニューの使用方法を示しています(この場合、モデルは無関係であるため、QObjectリストを使用していません。したがって、例を単純化するためにListModelを使用します)
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 1.4 as QQC1
import QtQuick.Controls 2.14 as QQC2
Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel {
id: libraryModel
ListElement {
title: "A Masterpiece"
author: "Gabriel"
}
ListElement {
title: "Brilliance"
author: "Jens"
}
ListElement {
title: "Outstanding"
author: "Frederik"
}
}
QQC1.TableView {
id: tableview
x:100
y:100
width: 400
height: 400
model: libraryModel
QQC1.TableViewColumn {
role: "title"
title: "Title"
width: 100
}
QQC1.TableViewColumn {
role: "author"
title: "Author"
width: 200
}
MouseArea{
anchors.fill: parent
propagateComposedEvents: true
acceptedButtons: Qt.RightButton
onClicked: {
var row = tableview.rowAt(mouseX, mouseY)
if(row >= 0){
console.log(row)
tableview.currentRow = row
tableview.selection.clear()
tableview.selection.select(row, row)
contextMenu.popup()
}
}
}
}
QQC2.Menu {
id: contextMenu
QQC2.MenuItem {
text: "Delete"
onClicked: libraryModel.remove(tableview.currentRow)
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加