<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:arial,helvetica,sans-serif;font-size:10pt"><div>I posted this to qt centre first a couple of weeks ago but have not been able to get an answer hopefully someone here might be able to pass on a clue<br><span><a target="_blank" href="http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-change-the-selection-of-a-parent-qtableview-from-a-sub-qtableview-25996.html">http://www.qtcentre.org/forum/f-qt-programming-2/t-how-to-change-the-selection-of-a-parent-qtableview-from-a-sub-qtableview-25996.html</a></span><br><br><br>I have a special table that's related to it's self like so<br>
<div class="highlight" style="margin: 5px 20px 20px;">
        <div class="smallfont" style="margin-bottom: 2px;">Qt Code:</div>
        <div class="alt2" style="border: 1px solid black; margin: 0px; padding: 6px; overflow: auto; width: 90%; height: 162px;"><div dir="ltr" style="text-align: left;"><table class="qt" style="font-family: monospace;"><tbody><tr class="li1"><td class="ln"><pre class="de1">1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br></pre></td><td class="de1"><pre class="de1">CREATE TABLE people <span class="br0">(</span> <br>ID INTEGER PRIMARY KEY AUTOINCREMENT <br>, Name TEXT <br>, BlaBla TEXT<span class="br0">)</span><br> <br>CREATE TABLE children <span class="br0">(</span> <br>parentID INTEGER <br>, childID INTEGER <br>, Whatever TEXT<span class="br0">)</span></pre></td></tr></tbody></table></div></div>
</div>so I'd like to have 1 QTableView that's a master list and lists
all people. I'd like a sub QTableView that will list only the children
of the selected person. So far no big deal. What I can't figure out is
how to make it so that when the user double clicks on a child that
child becomes the new selection of the master list of all people.<br>
<br>
I've hacked together a quick example of what I'm talking about that
works OK on my machine using python 2.6 and pyqt 4.5.3 though this
should be general to all qt more or less. In the example it's the
childrenClicked method that I can't figure out.<br>
<br>
Thanks,<br>
Aaron<br><br><br>from PyQt4.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel,<br> QSqlRelation, QSqlRelationalTableModel)<br>from PyQt4.QtGui import (QApplication, QMainWindow)<br>from PyQt4 import QtCore, QtGui<br>from PyQt4.QtCore import (QObject, SIGNAL, SLOT, QVariant, Qt)<br>from PyQt4.QtCore import *<br>from PyQt4.QtGui import *<br> <br>class PeopleTableModel(QSqlTableModel):<br> def __init__(self):<br> QSqlTableModel.__init__(self)<br> self.setTable("people")<br> <br> self.setHeaderData(0, Qt.Horizontal, QVariant("ID"))<br> self.setHeaderData(1, Qt.Horizontal,
QVariant("Name"))<br> self.setHeaderData(2, Qt.Horizontal, QVariant("BlaBla"))<br> <br> self.setEditStrategy(QSqlTableModel.OnFieldChange)<br> <br> self.select()<br> <br>class ChildrenModel(QSqlRelationalTableModel):<br> def __init__(self):<br> QSqlRelationalTableModel.__init__(self)<br> <br> self.setTable("children")<br> <br> self.setRelation(0, QSqlRelation("people", "ID", "Name"))<br> self.setRelation(1, QSqlRelation("people", "ID",
"Name"))<br> self.setHeaderData(2, Qt.Horizontal, QVariant("Whatever"))<br> self.select()<br> <br>class aMainWindow(QMainWindow):<br> def __init__(self):<br> QMainWindow.__init__(self)<br> <br> self.setObjectName("MainWindow")<br> self.resize(714, 690)<br> <br> self.centralwidget = QtGui.QWidget(self)<br> self.centralwidget.setObjectName("centralwidget")<br> self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)<br>
self.horizontalLayout.setObjectName("horizontalLayout")<br> <br> self.peopleView = QtGui.QTableView(self)<br> self.peopleView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)<br> self.peopleView.setAlternatingRowColors(True)<br> self.peopleView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)<br> self.peopleView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)<br> self.horizontalLayout.addWidget(self.peopleView) <br> self.childrenView = QtGui.QTableView(self)<br>
self.childrenView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)<br> self.childrenView.setAlternatingRowColors(True)<br> self.childrenView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)<br> self.childrenView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) <br> self.horizontalLayout.addWidget(self.childrenView)<br> <br> self.setCentralWidget(self.centralwidget) <br> <br> <br> self.db = QSqlDatabase.addDatabase('QSQLITE')<br>
self.db.setDatabaseName(':memory:')<br> self.db.open()<br> <br> #I added extra columns just to be sure to show row selection not just cell.<br> query = QSqlQuery(self.db)<br> query.prepare('CREATE TABLE people ( \<br> ID INTEGER PRIMARY KEY AUTOINCREMENT \<br> , Name TEXT \<br> , BlaBla TEXT);')<br>
query.exec_() <br> query.prepare('CREATE TABLE children ( \<br> parentID INTEGER \<br> , childID INTEGER \<br> , Whatever TEXT);')<br> query.exec_()<br> <br> query.prepare('INSERT INTO people VALUES(?, ?, ?)')<br> ids = [1,2,3,4,5]<br> names = ['Mary', 'John', 'Sam', 'Kate',
'Mark']<br> bla = ['hmm', 'hmm', 'hmm', 'hmm', 'hmm']<br> <br> query.addBindValue(ids) <br> query.addBindValue(names) <br> query.addBindValue(bla)<br> query.execBatch() <br> <br> query.prepare('INSERT INTO children VALUES(?, ?, ?)')<br> parents = [1,1,2,2]<br> children = [4,5,1,3]<br> yada = ['hmm', 'hmm', 'hmm', 'hmm']<br> <br>
query.addBindValue(parents) <br> query.addBindValue(children) <br> query.addBindValue(yada)<br> query.execBatch()<br> <br> self.peopleModel = PeopleTableModel()<br> self.peopleView.setModel(self.peopleModel)<br> self.peopleView.setColumnHidden(0, True)<br> self.childrenModel = ChildrenModel()<br> self.childrenView.setModel(self.childrenModel)<br> self.childrenView.setColumnHidden(0, True)
<br> <br> QObject.connect(self.peopleView.selectionModel(),<br> SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),<br> self.peopleChanged)<br> <br> QObject.connect(self.childrenView,<br> SIGNAL("doubleClicked(QModelIndex)"),<br> self.childrenClicked)<br> <br> self.childrenView.horizontalHeader().moveSection(1,0)<br>
self.peopleChanged(self.peopleView.currentIndex())<br> <br> <br> def peopleChanged(self, index):<br> if index.isValid():<br> record = self.peopleModel.record(index.row())<br> id = record.value("ID").toInt()[0]<br> self.childrenModel.setFilter(QString("parentID = %1").arg(id))<br> else:<br> self.childrenModel.setFilter("parentID = -1")<br> #self.childrenModel.reset() # workaround for Qt <= 4.3.3/SQLite bug<br>
self.childrenModel.select()<br> self.childrenView.horizontalHeader().setVisible(<br> self.childrenModel.rowCount() > 0) <br> <br> def childrenClicked(self, index):<br> print 'childrenClicked'<br> #newIndex = index.sibling(3,0)<br> #self.tableView.selectionModel().select(newIndex)<br> print index<br> record = self.childrenModel.record(index.row())<br> print record<br> print record.value(1).toString()<br>
<br> #works but how do I get the row from the childs index?<br> self.peopleView.selectRow(4)<br> <br> #seems to select (look at row numbers carefully) but doesn't change children.<br> #self.peopleView.selectionModel().select(self.peopleModel.index(1,0), QItemSelectionModel.SelectCurrent)<br> <br> <br>if __name__ == "__main__":<br> import sys<br> app = QApplication(sys.argv)<br> MainWindow = aMainWindow()<br> <br> MainWindow.show()<br> sys.exit(app.exec_())<br></div>
</div><br>
</body></html>