[PyQt] QSortFilterProxyModel.createIndex from within a slot callback

Zorg 421 zorglub421 at gmail.com
Fri Feb 27 14:15:05 GMT 2009


Hello PyQt users, I do not know how to use the third argument of createIndex
in the following code.
It's in the callback of a tree view, populated by a FilterProxyModel.

I want to use the value of the first column of the view when I select a
line, even if I click on another column.

tree view setting for a single, whole line selection:

        self.treeView.SelectionMode = QtGui.QTreeView.SingleSelection
        self.treeView.SelectionBehavior = QtGui.QTreeView.SelectRows

My difficulty is in the onTableRowSelected method.
any help appreciated.


----


class MainWindow(QtGui.QMainWindow):
    def __init__(self, dbh):
        QtGui.QMainWindow.__init__(self)
        self.dbh = dbh
        self.createActions()
        self.createMenus()
        self.createTopIP_Table()
        self.proxyModel.setSourceModel(createNF_SummaryModel(self, dbh))
        self.setCentralWidget(self.proxyGroupBox)
        self.createMplFrame1()
        self.createToolBars()
        self.createStatusBar()
        self.createDockWindows()

...

    def onTableRowSelected(self, index):
        if index.isValid():
            print index.row(), index.column()
            #index_ip = self.proxyModel.createIndex(index.row(), 0,
self.proxyModel)
            index_ip = self.proxyModel.createIndex(index.row(), 0, 0)
<----------- ????
            qvar = self.proxyModel.itemData(index_ip)
            qst_ip_addr = qvar[0].toString()
            print qst_ip_addr
            self.onDraw(qst_ip_addr)
        else:
            print "WW: invalid index in table model!"

    def createTopIP_Table(self):
        self.proxyModel = QtGui.QSortFilterProxyModel()
        self.proxyModel.setDynamicSortFilter(True)
        self.selectionModel = QtGui.QItemSelectionModel(self.proxyModel)
        self.treeView = QtGui.QTreeView()
        self.treeView.setModel(self.proxyModel)
        self.treeView.setSelectionModel(self.selectionModel)
        self.treeView.SelectionMode = QtGui.QTreeView.SingleSelection
        self.treeView.SelectionBehavior = QtGui.QTreeView.SelectRows
        self.connect(self.treeView.selectionModel(),
QtCore.SIGNAL("currentRowChanged(QModelIndex, QModelIndex)"),
self.onTableRowSelected)
        self.treeView.setSortingEnabled(True)
        self.treeView.sortByColumn(2, QtCore.Qt.DescendingOrder)
        self.treeView.setRootIsDecorated(False)
        self.treeView.setAlternatingRowColors(True)
        self.proxyGroupBox = QtGui.QGroupBox("blah blah")
        proxyLayout = QtGui.QGridLayout()
        proxyLayout.addWidget(self.treeView, 0, 0, 1, 7)
        self.proxyGroupBox.setLayout(proxyLayout)

def addIP(model, ...):
    model.insertRow(0)
    model.setData(model.index(0, 0), QtCore.QVariant(ip))
    model.setData(model.index(0, 3), QtCore.QVariant(pkt_in))
    model.setData(model.index(0, 4), QtCore.QVariant(pkt_out))
    model.setData(model.index(0, 5), QtCore.QVariant(byte_in))
    model.setData(model.index(0, 6), QtCore.QVariant(byte_out))

def createNF_SummaryModel(parent, dbh):
    model = QtGui.QStandardItemModel(0, 7, parent)
    model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant("IP
Address"))
    model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant("flows/sec
in"))
    model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant("flows/sec
out"))
    model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant("pkts/sec
in"))
    model.setHeaderData(4, QtCore.Qt.Horizontal, QtCore.QVariant("pkts/sec
out"))
    model.setHeaderData(5, QtCore.Qt.Horizontal, QtCore.QVariant("bytes/sec
in"))
    model.setHeaderData(6, QtCore.Qt.Horizontal, QtCore.QVariant("bytes/sec
out"))
    scale = 1.0/300.0
    req = "SELECT blah blah"
    cdb = dbh.cursor()
    cdb.execute(req)
    res = cdb.fetchone()
    req = """SELECT blah blah""" % res[0]
    cdb.execute(req)
    res = cdb.fetchall()
    for (....) in res:
        addIP(model, ...)
    cdb.close()
    return model
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20090227/93d24ac2/attachment.html


More information about the PyQt mailing list