Home > Mobile >  selecting next/previowus record in Qt TableView
selecting next/previowus record in Qt TableView

Time:07-27

I have the same problem as was presented here: How to select next row automatically in Qt tableView whenever a pushbutton is pressed?

but I use Qt platform with python (qgis.PyQt), so I can't translate one-to-one as in presented above (where is c ). Can you see what in code from attached below is wrong?

def NextOne(self):
    #ind = self.dlg.tableView_3.selectionModel().selectedIndexes()
    self.dlg.tableView_3.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
    self.dlg.tableView_3.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
    model = QStandardItemModel()
    self.dlg.tableView_3.setModel(model)
    selModel = QItemSelectionModel()
    selModel.setModel(model)
    self.dlg.tableView_3.setSelectionModel(selModel)
    ind=selModel.selectedIndexes()
    print (ind)
    if (ind.isEmpty()):
        return
    else:
        selModel.select(QModelIndex(), model.SelectionFlag.clear)
        row = selectionModel.selection().indexes().row()
        print (row)
        for i in ind:
            nast = i.sibling(i.row() 1,i.colmun())
            selModel.select(nast,model.SelectionFlag.Select)
    return

CodePudding user response:

It says that the error you are getting is: if (ind.isEmpty()): 'list' object has no attribute 'isEmpty'.

QModelIndexList QItemSelectionModel::selectedIndexes() const returns a QList.

Since the QList gets bound to the native python list, this is what you should write in python to have idiomatic python:

if not ind:
    return

CodePudding user response:

Ok, In the meanwhile i found better code:

ind= self.dlg.tableView_3.selectionModel()#.selectedRows()
    ind2= ind.currentIndex()
    if ind2:
        next1 = ind2.siblingAtRow(ind2.row()   1)
        self.dlg.tableView_3.selectionModel().select(next1,ind.ClearAndSelect | ind.Rows)
        self.SelekcjaRzedu()
    else:
        next1 = ind2.model().index()
        self.dlg.tableView_3.selectRow(0)

but I work only for 1st next record in TableView. selModel doesn't exist in it, wheras selcetionModel yes.

  • Related