à tous,

Ci-dessous un bout de code me permettant d'ajouter dynamiquement des lignes dans un TableView, d'enregistrer le contenu dans un ListModel et éventuellement de supprimer des lignes.

Mon souci est lié à la suppression d'une ligne. Le code présenté supprime bien la ligne voulue en supprimant le contenu d'un élément du ListModel (remove(index)).

Cependant si je souhaite par la suite rajouter une ligne, c'est la copie de la ligne supprimée qui réapparaît au lieu d'une ligne vide et je ne vois pas du tout d'où cela peut venir...


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.3
 
ApplicationWindow {
    width: 600
    height: 300
    minimumHeight: height
    maximumHeight: height
    minimumWidth: width
    maximumWidth: width
    visible : true
 
    property var comboModel : [{piece:"Item 1", j:"10"}, {piece:"Item 2", j:"20"}, {piece:"Item 3", j:"30"}, {piece:"Item 4", j:"40"}]
 
 
    GridLayout {
        id : grid
        anchors.fill: parent
        anchors.margins: 5
        columns: 4
        TableView{
            id : table
            Layout.columnSpan: grid.columns
            Layout.fillWidth: true
            Layout.fillHeight: true
            //TableViewColumn {role:"selection" ; title : "" ; width : table.width*0.05 ; delegate : checkDelegate}
            TableViewColumn {role:"selection" ; title : "" ; width : table.width*0.05 ; delegate : btnDelegate}
            TableViewColumn {role:"item" ; title : "Élément" ; width : table.width*0.8-2 ; delegate : comboDelegate}
            TableViewColumn {role:"qte" ; title : "Quantité" ; width : table.width*0.15 ; delegate : editableDelegate}
            rowDelegate : Rectangle{id : rect ;height: 23}
            model : aspiModel
 
        }
 
        Component {
            id: editableDelegate
            TextField {
                id : textinput
                onTextChanged{
                    aspiModel.setProperty(styleData.row,styleData.role, text)
                }
                onAccepted{
                    textinput.focus = false
                    aspiModel.setProperty(styleData.row,styleData.role, text)
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked{
                        textinput.forceActiveFocus()
                    }
                }
            }
        }
 
        Component {
            id: comboDelegate
            ComboBox {
                id : combo
                model: comboModel
                textRole : "piece"
                onCurrentTextChanged:aspiModel.setProperty(styleData.row,styleData.role, currentText)
            }
        }
        Component {
            id: checkDelegate
 
            CheckBox {
                id : check
                onCheckedChanged: aspiModel.setProperty(styleData.row,styleData.role, checked)
            }
        }
        Component {
            id: btnDelegate
 
            Button {
                id : id
                text : "X"
                onClicked{
                    console.log("Avant remove :", aspiModel.count, " entrées")
                    aspiModel.remove(styleData.row)
                    console.log("Après remove :", aspiModel.count, " entrées")
                }
            }
        }
 
        ListModel {
            id:aspiModel
        }
 
        RowLayout {
            Layout.columnSpan: grid.columns
            Layout.fillWidth: true
            Button {
                text : "Ajouter un élément"
                Layout.fillWidth: true
                implicitHeight: 40
                onClicked: aspiModel.append({})
            }
        }
    }
}
Une idée ???

D'avance merci.

J