Getting data from selected item in QListView

后端 未结 3 682
北恋
北恋 2021-01-03 12:45

I populate a QListView with QSqlTableModel.

  projectModel = QSqlTableModel()
  projectModel.setTable(\"project\")
  projectModel.select()

  projectView = Q         


        
3条回答
  •  清歌不尽
    2021-01-03 13:31

    You can use the selectedIndexes() method of the model to get the indexes of the selected items using something like

    idx = self.projectView.selectionModel().selectedRows()
    

    then looping on idx, you can get the values you need with

    for rec in idx:
        entry = rec.row()
        val = self.projectModel.record(entry).field(x).value()
    

    with x beeing the number of the column you want to retrive

    val is now a QVariant and you can further elaborate it.

提交回复
热议问题