I'm trying to make a QAbstractListView for use with QComboBox that it maintains sorted list of items. I've included some sample code below that shows my problem. When I update the items in the list, the combo box does not update to display changes in the current index model. I have tried experimenting about the rows that have been synchronized in the buttboined and rows, but I do not see any effect (maybe I'm doing it wrong?).
My actual use case is a bit more complicated, but the example should be quite a lot. The items being sorted are not just strings, and do a little more effort to separate the item data for your display roll.
Item added and items removed are my own work, which are linked to the signal, the second list which I am trying to proxy.
To trigger the problem, press the 'Insert' c '' button. The string is inserted into the correct list, but the selection runs from 'E' to 'D' (ie the selection does not change in the index).
#! / Usr / bin / env python # - * - coding: UTF -8 - * - PyQt4 import QtCore, QtGui class model (QtCore.QAbstractListModel): def __init __ (self, * args, ** kwargs): QtCore.QAbstractListModel .__ init __ (self, * args, ** kwargs) self.items = [] def rOWCOUNT (self, parent = QtCore.QModelIndex ()): return lane (self.items) def data (self, index, role = QtCore.Qt.DisplayRole): If index.isValid () true if the role == QtCore.Qt.DisplayRole: return QtCore.QVariant (self.items [index.row ()]) elif role == QtCore .Qt.ItemDataRole: Return QtCore.QVariant (self.items [index.row ()]) QtCore.QVariant return () Def items added (auto, item): # items Micro insert the status items your sort = Sort (item) line = 0 the line & lt; Lane (Self. Items) and lane (item) & gt; 0: If the item [0] Self.items [row]: self.items [row: row] = [items.pop (0)] line + = 1 line + = 1 # Add the remaining items at the end of the list if len (items) & gt; 0: self.items.extend (item) def item removed (auto, item): for items in item # Remove items from list: range (0, lane (self.items)) for the quote: if self.items [line] == item: Selfkitemskpop (line) def main (break): app = QtGui.QApplication ([]) w = QtGui.QWidget () w.resize (300,300) layout = QtGui.QVBoxLayout () model = model () model.itemsAdded ([ 'a', 'b', 'd', 'e']) was telling = QtGui.QComboBox () combobox.setModel (model) combobox.setCurrentIndex (3) layout.addWidget (Told) DEF insertC (self): model.itemsAdded ('C') button = QtGui.QPushButton ('Insert' 'C' ') button.clicked.connect (insertC) layout.addWidget (B) N) w.setLayout (layout) w.show () App.exec_ () if __name__ == '__main__': main ()
I think you need to modify the selection indicator yourself, such as
if the line You should read this:
Models that provide an interface for a restructuring list-like data structures, include insertRows () and removeRows () Can provide implementation of. When implementing these tasks, it is important to call suitable functions so that all connected ideas are aware of any changes:
one ?? ¢ should start an insertRows () implementation before inserting new rows in the INnsertRows (), and then it will finally call the interval () immediately.
one ?? ¢ A removable rows () implementation should be started before ramvov () Rows are removed from the data structure, and it is necessary to call the end RemovesRows () immediately afterwards.
No comments:
Post a Comment