Results 1 to 3 of 3

Thread: how to change the selection of a parent QTableView from a sub QTableView

  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Platforms
    Windows

    Default how to change the selection of a parent QTableView from a sub QTableView

    I have a special table that's related to it's self like so
    Qt Code:
    1. CREATE TABLE people (
    2. ID INTEGER PRIMARY KEY AUTOINCREMENT
    3. , Name TEXT
    4. , BlaBla TEXT)
    5.  
    6. CREATE TABLE children (
    7. parentID INTEGER
    8. , childID INTEGER
    9. , Whatever TEXT)
    To copy to clipboard, switch view to plain text mode 

    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.

    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.

    Thanks,
    Aaron



    Qt Code:
    1. from PyQt4.QtSql import (QSqlDatabase, QSqlQuery, QSqlTableModel,
    2. from PyQt4.QtGui import (QApplication, QMainWindow)
    3. from PyQt4 import QtCore, QtGui
    4. from PyQt4.QtCore import (QObject, SIGNAL, SLOT, QVariant, Qt)
    5. from PyQt4.QtCore import *
    6. from PyQt4.QtGui import *
    7.  
    8. class PeopleTableModel(QSqlTableModel):
    9. def __init__(self):
    10. QSqlTableModel.__init__(self)
    11. self.setTable("people")
    12.  
    13. self.setHeaderData(0, Qt.Horizontal, QVariant("ID"))
    14. self.setHeaderData(1, Qt.Horizontal, QVariant("Name"))
    15. self.setHeaderData(2, Qt.Horizontal, QVariant("BlaBla"))
    16.  
    17. self.setEditStrategy(QSqlTableModel.OnFieldChange)
    18.  
    19. self.select()
    20.  
    21. class ChildrenModel(QSqlRelationalTableModel):
    22. def __init__(self):
    23. QSqlRelationalTableModel.__init__(self)
    24.  
    25. self.setTable("children")
    26.  
    27. self.setRelation(0, QSqlRelation("people", "ID", "Name"))
    28. self.setRelation(1, QSqlRelation("people", "ID", "Name"))
    29. self.setHeaderData(2, Qt.Horizontal, QVariant("Whatever"))
    30. self.select()
    31.  
    32. class aMainWindow(QMainWindow):
    33. def __init__(self):
    34. QMainWindow.__init__(self)
    35.  
    36. self.setObjectName("MainWindow")
    37. self.resize(714, 690)
    38.  
    39. self.centralwidget = QtGui.QWidget(self)
    40. self.centralwidget.setObjectName("centralwidget")
    41. self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
    42. self.horizontalLayout.setObjectName("horizontalLayout")
    43.  
    44. self.peopleView = QtGui.QTableView(self)
    45. self.peopleView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
    46. self.peopleView.setAlternatingRowColors(True)
    47. self.peopleView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
    48. self.peopleView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
    49. self.horizontalLayout.addWidget(self.peopleView)
    50. self.childrenView = QtGui.QTableView(self)
    51. self.childrenView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
    52. self.childrenView.setAlternatingRowColors(True)
    53. self.childrenView.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
    54. self.childrenView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
    55. self.horizontalLayout.addWidget(self.childrenView)
    56.  
    57. self.setCentralWidget(self.centralwidget)
    58.  
    59.  
    60. self.db = QSqlDatabase.addDatabase('QSQLITE')
    61. self.db.setDatabaseName(':memory:')
    62. self.db.open()
    63.  
    64. #I added extra columns just to be sure to show row selection not just cell.
    65. query = QSqlQuery(self.db)
    66. query.prepare('CREATE TABLE people ( \
    67. ID INTEGER PRIMARY KEY AUTOINCREMENT \
    68. , Name TEXT \
    69. , BlaBla TEXT);')
    70. query.exec_()
    71. query.prepare('CREATE TABLE children ( \
    72. parentID INTEGER \
    73. , childID INTEGER \
    74. , Whatever TEXT);')
    75. query.exec_()
    76.  
    77. query.prepare('INSERT INTO people VALUES(?, ?, ?)')
    78. ids = [1,2,3,4,5]
    79. names = ['Mary', 'John', 'Sam', 'Kate', 'Mark']
    80. bla = ['hmm', 'hmm', 'hmm', 'hmm', 'hmm']
    81.  
    82. query.addBindValue(ids)
    83. query.addBindValue(names)
    84. query.addBindValue(bla)
    85. query.execBatch()
    86.  
    87. query.prepare('INSERT INTO children VALUES(?, ?, ?)')
    88. parents = [1,1,2,2]
    89. children = [4,5,1,3]
    90. yada = ['hmm', 'hmm', 'hmm', 'hmm']
    91.  
    92. query.addBindValue(parents)
    93. query.addBindValue(children)
    94. query.addBindValue(yada)
    95. query.execBatch()
    96.  
    97. self.peopleModel = PeopleTableModel()
    98. self.peopleView.setModel(self.peopleModel)
    99. self.peopleView.setColumnHidden(0, True)
    100. self.childrenModel = ChildrenModel()
    101. self.childrenView.setModel(self.childrenModel)
    102. self.childrenView.setColumnHidden(0, True)
    103.  
    104. QObject.connect(self.peopleView.selectionModel(),
    105. SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),
    106. self.peopleChanged)
    107.  
    108. QObject.connect(self.childrenView,
    109. SIGNAL("doubleClicked(QModelIndex)"),
    110. self.childrenClicked)
    111.  
    112. self.childrenView.horizontalHeader().moveSection(1,0)
    113. self.peopleChanged(self.peopleView.currentIndex())
    114.  
    115.  
    116. def peopleChanged(self, index):
    117. if index.isValid():
    118. record = self.peopleModel.record(index.row())
    119. id = record.value("ID").toInt()[0]
    120. self.childrenModel.setFilter(QString("parentID = %1").arg(id))
    121. else:
    122. self.childrenModel.setFilter("parentID = -1")
    123. #self.childrenModel.reset() # workaround for Qt <= 4.3.3/SQLite bug
    124. self.childrenModel.select()
    125. self.childrenView.horizontalHeader().setVisible(
    126. self.childrenModel.rowCount() > 0)
    127.  
    128. def childrenClicked(self, index):
    129. print 'childrenClicked'
    130. #newIndex = index.sibling(3,0)
    131. #self.tableView.selectionModel().select(newIndex)
    132. print index
    133. record = self.childrenModel.record(index.row())
    134. print record
    135. print record.value(1).toString()
    136.  
    137. #works but how do I get the row from the childs index?
    138. self.peopleView.selectRow(4)
    139.  
    140. #seems to select (look at row numbers carefully) but doesn't change children.
    141. #self.peopleView.selectionModel().select(self.peopleModel.index(1,0), QItemSelectionModel.SelectCurrent)
    142.  
    143.  
    144. if __name__ == "__main__":
    145. import sys
    146. app = QApplication(sys.argv)
    147. MainWindow = aMainWindow()
    148.  
    149. MainWindow.show()
    150. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 162 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to change the selection of a parent QTableView from a sub QTableView

    Well, unless you query the "parent's row" in the sql query (so the child record would contain the row already) you will have to search your people model for the entry selected in the children model.

  3. #3
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: how to change the selection of a parent QTableView from a sub QTableView

    yes exactly but I can't figure out how to do that. The children model only returns the values not the ids and even if I had the id (column value) I don't know how to get the index of a specific row from a db value. like I said I can't seem to connect the two halves in that one method.

Similar Threads

  1. How to change columns order in a QTableView...
    By cydside in forum Qt Programming
    Replies: 1
    Last Post: 20th April 2009, 10:42
  2. Catch a row selection event in QTableView
    By Windsoarer in forum Qt Programming
    Replies: 1
    Last Post: 2nd March 2009, 22:44
  3. QSqlTableModel and QTableView selection problems
    By innerhippy in forum Qt Programming
    Replies: 5
    Last Post: 19th December 2008, 05:48
  4. QTableView Row Selection
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 12th December 2007, 21:27
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.