Hi Mads,
You might take a look at QTableView.setRowHidden().
I'm testing something along similar lines. (See my displayTableRows()
method in the attached file.) While my working code gets its
displayable rows from a second SQL query, in your case, it wouldn't
take much to glean the info from a selectionModel.
Though if anyone has a better suggestion, I'm all ears.
Scott
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# imports
#-------------------------------------------------------------------------------
import sys, os
from PyQt4 import QtCore, QtGui, QtSql
#-------------------------------------------------------------------------------
# enum
#-------------------------------------------------------------------------------
COLOR_ID, NAME, DESCRIPTION = range(3)
#-------------------------------------------------------------------------------
# schema
#-------------------------------------------------------------------------------
def createFakeData():
query = QtSql.QSqlQuery()
query.exec_("DROP TABLE color")
query.exec_("""CREATE TABLE color (
color_id PRIMARY KEY,
name VARCHAR(32) UNIQUE NOT NULL,
description TEXT NOT NULL)""")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (1, 'red', 'this is red')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (2, 'vermillion', 'this is vermillion')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (2, 'rose madder', 'this is rose madder')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (3, 'green', 'this is green')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (4, 'phthalocyanine', 'this is phthalocyanine')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (5, 'leaf', 'this is leaf')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (6, 'blue', 'this is blue')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (7, 'ultramarine', 'this is ultramarine')")
query.exec_("INSERT INTO color (color_id, name, description) "
"VALUES (8, 'cerulean', 'this is cerulean')")
#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class MyTable(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self)
# init
# ------------------------------------------------
self.theModel = QtSql.QSqlRelationalTableModel(self)
self.rowCount = 0
self.displayList = []
# table view
# ------------------------------------------------
self.theView = QtGui.QTableView()
self.theView.setSelectionMode(QtGui.QTableView.SingleSelection)
self.theView.setSelectionBehavior(QtGui.QTableView.SelectRows)
self.theView.resizeColumnsToContents()
self.theView.setAlternatingRowColors(True)
self.theView.horizontalHeader().setStretchLastSection(True)
def setModel(self, theModel):
self.theModel = theModel
self.theView.setModel(self.theModel)
self.rowCount = self.theModel.rowCount()
self.theView.setColumnHidden(COLOR_ID, True)
def setDisplayList(self, theList):
self.displayList = theList
def displayTableRows(self):
rowCount = self.rowCount
for row in range(rowCount):
itemId = self.theModel.data(self.theModel.index(row, 0)).toInt()[0]
# set each row visible
self.theView.setRowHidden(row, False)
if itemId not in self.displayList:
print itemId, self.displayList
self.theView.setRowHidden(row, True)
#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class MvTestForm(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self)
# table model
# ------------------------------------------------
self.theModel = QtSql.QSqlTableModel(self)
self.theModel.setTable("color")
self.theModel.setSort(COLOR_ID, QtCore.Qt.AscendingOrder)
self.theModel.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
# column headers
self.theModel.setHeaderData(COLOR_ID, QtCore.Qt.Horizontal, QtCore.QVariant("COLOR_ID"))
self.theModel.setHeaderData(NAME, QtCore.Qt.Horizontal, QtCore.QVariant("NAME"))
self.theModel.setHeaderData(DESCRIPTION, QtCore.Qt.Horizontal, QtCore.QVariant("DESCRIPTION"))
select = self.theModel.select()
# top table view
# ------------------------------------------------
self.topView = MyTable()
self.topView.setModel(self.theModel)
self.topView.setDisplayList([2, 4, 6])
self.topView.displayTableRows()
# mid table view
# ------------------------------------------------
self.midView = MyTable()
self.midView.setModel(self.theModel)
self.midView.setDisplayList([1, 3, 5, 7])
self.midView.displayTableRows()
# bot table view
# ------------------------------------------------
self.botView = MyTable()
self.botView.setModel(self.theModel)
# table layout
# ------------------------------------------------
self.tableLayout = QtGui.QVBoxLayout()
self.tableLayout.addWidget(self.topView.theView)
self.tableLayout.addWidget(self.midView.theView)
self.tableLayout.addWidget(self.botView.theView)
self.setLayout(self.tableLayout)
#-------------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
filename = os.path.join(os.path.dirname(__file__), "test.db")
create = not QtCore.QFile.exists(filename)
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(filename)
if not db.open():
QtGui.QMessageBox.warning(None, "Test",
QtCore.QString("Database Error: %1").arg(db.lastError().text()))
sys.exit(1)
ok = db.open()
createFakeData()
form = MvTestForm()
form.setWindowTitle("Model View Test")
form.show()
sys.exit(app.exec_())
On Mar 16, 2010, at 1:23 PM, Mads Ipsen wrote:
Dear Gurus,
Suppose I have a table model that I view in a QTableView (A). In
another QTableView (B) I want to display the rows that are selected
in the first QTableView (A).
How do I achieve this? Ie, each selected row in view (A) should be
displayed in view (B).
Best regards,
Mads
--
+-------------------------------------------------------------+
| Mads Ipsen, Scientific developer |
+-------------------------------+-----------------------------+
| QuantumWise A/S | phone: +45-29716388 |
| Nørre Søgade 27A | www: www.quantumwise.com |
| DK-1370 Copenhagen K, Denmark | email: [email protected] |
+-------------------------------+-----------------------------+
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt
Scott Frankel
President
Circle-S Studios
510-339-7477 (o)
510-332-2990 (c)
www.circlesfx.com
www.sequoyaworkflow.com
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt