Charles Brunet
Charles Brunet

Reputation: 23160

What is parent in PySide QSelectionModel.isRowSelected() function?

I need to determine which rows are selected in a QTableView associated with a QStandardItemModel. From the view, I call the function selectionModel() to get the selection. This function returns a QSelectionModel object. From that object, I want to call isRowSelected() function. This function takes two arguments: the row I want to test, and a parent argument, which is a QModelIndex. This is where I'm lost. What is this parent argument for? Where does it come from? Conceptually, I don't understand why I'd need this parameter, and concretely, I don't know what value I should pass to the function to make it work.

Upvotes: 2

Views: 584

Answers (1)

user1006989
user1006989

Reputation:

You'll find the parent useful in a QTreeView, for example. For your use case, this are the relevant parts of the documentation:

The index is used by item views, delegates, and selection models to locate an item in the model
...
Invalid indexes are often used as parent indexes when referring to top-level items in a model."

With QtCore.QModelIndex() you'll create an invalid index, that's the argument you are looking for. In this example, you can use the context menu to print the selection state of the rows:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from PyQt4 import QtGui, QtCore

class MyWindow(QtGui.QTableView):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.modelSource = QtGui.QStandardItemModel(self)

        for rowNumber in range(3):
            items = []
            for columnNumber in range(3):
                item = QtGui.QStandardItem()
                item.setText("row: {0} column: {0}".format(rowNumber, columnNumber))

                items.append(item)

            self.modelSource.appendRow(items)

        self.actionSelectedRows = QtGui.QAction(self)
        self.actionSelectedRows.setText("Get Selected Rows")
        self.actionSelectedRows.triggered.connect(self.on_actionSelectedRows_triggered)

        self.contextMenu = QtGui.QMenu(self)
        self.contextMenu.addAction(self.actionSelectedRows)

        self.setModel(self.modelSource)
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self.horizontalHeader().setStretchLastSection(True)
        self.customContextMenuRequested.connect(self.on_customContextMenuRequested)

    @QtCore.pyqtSlot(bool)
    def on_actionSelectedRows_triggered(self, state):
        for rowNumber in range(self.model().rowCount()):
            info = "Row {0} is ".format(rowNumber)
            if self.selectionModel().isRowSelected(rowNumber, QtCore.QModelIndex()):
                info += "selected"

            else:
                info += "not selected"

            print info

    @QtCore.pyqtSlot(QtCore.QPoint)
    def on_customContextMenuRequested(self, pos):
        self.contextMenu.exec_(self.mapToGlobal(pos))

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(333, 222)
    main.show()

    sys.exit(app.exec_())

Upvotes: 1

Related Questions