2016-05-25 10 views
0

QtQuick TableViewを使用してQSqlTableModelおよびQSortFilterProxyModelを介してデータベースからデータを表示しています。QtQuick TableViewの削除行が機能しない

行の削除操作が正しく動作しません。 QSortFilterProxyModelから派生したクラスでメソッドを実装して、QSortFilterProxyModelのremoveRowsメソッドを呼び出しました。

QSortFilterProxyModel(私はテキストボックスを使用して設定)でフィルタを設定している限り、すべて正常に動作します。ただし、フィルタが空の場合、TableViewのrowCountプロパティは減少せず、各削除後にcurrentRowプロパティがrowCount-2に設定されます。どうして?私にはバグのように見えます。なぜフィルターが空でないときに機能するのですか?

 Q_INVOKABLE void eliminaCliente(int row) { 
      removeRows(row,1); 
     } 

import QtQuick 2.6 
import QtQuick.Controls 1.5 
import QtQuick.Layouts 1.3 
import QtQuick.Dialogs 1.2 
import Material 0.2 
import Material.ListItems 0.1 

ApplicationWindow { 
    id: root 
    visible: true 
    width: 1024 
    height: 640 
    title: qsTr("assiBase") 

    Page { 
     id: pLayout 
     anchors.fill: parent 

     ColumnLayout { 
      anchors.fill: parent 

      Toolbar { 
       id: aBar 
       Layout.fillWidth: true 
       page: pLayout 
       backgroundColor: "#eeeeee" 

       RowLayout { 
        anchors.fill: parent 

        ActionButton { 
         id: addButton 
         Layout.leftMargin: 10 
         iconName: "content/add_circle" 
         backgroundColor: "#4CAF50" 
         onClicked: modalDialog.show() 
         isMiniSize: true 
        } 

        ActionButton { 
         id: editButton 
         iconName: "content/create" 
         isMiniSize: true 
        } 

        ActionButton { 
         id: deleteButton 
         iconName: "action/delete" 
         isMiniSize: true 
         backgroundColor: "#FF0000" 
         onClicked: { 
          if (dataView.currentRow != -1) { 
           var r = dataView.currentRow 
           console.log(dataView.currentRow) 
           sqlSortedData.eliminaCliente(dataView.currentRow) 
           console.log(dataView.rowCount) 
           //dataView.currentRow = r 

          } 
         } 
        } 

        RowLayout { 
         Layout.alignment: Qt.AlignRight 

         Icon { 
          name: "action/search" 
          Layout.alignment: Qt.AlignBottom 
         } 

         TextField { 
          id: searchBox 
          Layout.rightMargin: 20 
          Layout.minimumWidth: 400 
          Layout.preferredWidth: 500 
          placeholderText: qsTr("cerca...") 
          onTextChanged: sqlSortedData.setFilterWildcard(searchBox.text) 
          font.capitalization: Font.MixedCase 
         } 
        } 
       } 
      } 

      TableView { 
       anchors.top: aBar.bottom 
       anchors.topMargin: 3 
       sortIndicatorVisible: true 
       frameVisible: false 
       Layout.fillWidth: true 
       Layout.fillHeight: true 
       onSortIndicatorColumnChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder) 
       onSortIndicatorOrderChanged: model.sort(sortIndicatorColumn, sortIndicatorOrder) 
       id: dataView 

       TableViewColumn { 
        role: "ID" 
        visible: false 
       } 

       TableViewColumn { 
        role: "Nome" 
        title: "Nome" 
        width: 200 
       } 

       TableViewColumn { 
        role: "Residenza" 
        title: "Residenza" 
        width: 200 
       } 

       TableViewColumn { 
        role: "Assicurazione" 
        title: "Assicurazione" 
        width: 200 
       } 

       TableViewColumn { 
        width: 128 
        resizable: false 


        delegate: RowLayout { 
          anchors.fill: parent 
          clip: true 

          IconButton { 
           iconName: "content/create" 
           onClicked: console.log(styleData.row) 
          } 

          IconButton { 
           iconName: "action/delete" 
           onClicked: { 
            console.log(styleData.row) 
            sqlSortedData.eliminaCliente(styleData.row) 
            console.log(dataView.rowCount) 
           } 
          } 
        } 
       } 

       model: sqlSortedData 
      } 
     } 
    } 
+0

rootアイテムのcurrentRowを追跡してから、tableView.model.remove(root.currentRow)を試してください – Ashif

+0

返信いただきありがとうございます。しかし私はQSortFilterProxyModelが原因だと指摘しました。実際には、フィルタが空のときにrowCountは更新されません。したがって、問題はQSortFilterProxyModelにあり、TableViewにはありません。 – paolino

答えて

0

hereをご覧ください。回避策の提案があります。 QSortFilterProxyModelは長い間愛を必要としているようです。

関連する問題