Last active
December 17, 2015 00:39
-
-
Save Snegovikufa/5522379 to your computer and use it in GitHub Desktop.
In Archlinux prints None (PyQt4 version 4.10.1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import logging | |
import sys | |
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout) | |
logger = logging.getLogger("TestModel") | |
import sip | |
sip.setapi("QString", 2) | |
sip.setapi("QVariant", 2) | |
from PyQt4.QtGui import * | |
from PyQt4.QtCore import * | |
class ListModel(QAbstractListModel): | |
def __init__(self, items): | |
super(ListModel, self).__init__() | |
self._items = items | |
def data(self, index, role=None): | |
row = index.row() | |
if role == Qt.DisplayRole: | |
return self._items[row][0] | |
if role == Qt.UserRole + 1: | |
return self._items[row][1] | |
def rowCount(self, parentIndex=None): | |
return len(self._items) | |
def item(self, row, col=0): | |
return self._items[row][1] | |
def findItems(self, name): | |
found = [] | |
for j, item in enumerate(self._items): | |
name_, _ = item | |
if name_ == name: | |
found.append(j) | |
return found | |
def takeRow(self, row): | |
self.beginRemoveRows(QModelIndex(), row, row) | |
row = self._items.pop(row) | |
self.endRemoveRows() | |
return row | |
def appendRow(self, data): | |
self.beginInsertRows(QModelIndex(), len(self._items), 0) | |
if isinstance(data, (tuple, list)): | |
name, data = data | |
else: | |
name, data = data, data | |
self._items.append((name, data)) | |
self.endInsertRows() | |
def listModel (items, current = None) : | |
currentIndex = 0 | |
newitems = [] | |
for i, value in enumerate (items) : | |
if type (value) in [tuple, list] : | |
name, data = value[:2] | |
else : | |
name = value | |
data = value | |
newitems.append((name, data)) | |
if data == current : | |
currentIndex = i | |
model = ListModel(newitems) | |
return (model, currentIndex) | |
class CustomComboBox (QComboBox) : | |
currentChanged = pyqtSignal (object, name = "currentChanged") | |
def __init__ (self, items, current = None, handler = None) : | |
super (CustomComboBox, self).__init__ () | |
self.setItems (items, current = current) | |
self.connect (self, SIGNAL ("currentIndexChanged(int)"), self._currentChanged) | |
if handler is not None : | |
self.currentChanged.connect (handler) | |
def current (self) : | |
return self.model ().item (self.currentIndex ()) | |
def _currentChanged (self, idx) : | |
item = self.model ().item (idx) | |
if item is not None : | |
self.currentChanged.emit (item) | |
def setCurrent (self, data) : | |
for row in xrange (self.model ().rowCount ()) : | |
if self.model ().item (row, 0) == data : | |
self.setCurrentIndex (row) | |
return row | |
return None | |
def append (self, data) : | |
self.model ().appendRow (*data) | |
def remove (self, data) : | |
name, _data = data | |
rows = self.model ().findItems (name) | |
if len (rows) > 0 : | |
self.model ().takeRow (rows[0]) | |
def update (self, data) : | |
name, data = data | |
for i in xrange (self.count ()) : | |
if self.model ().item (i).data == data : | |
self.setItemText (i, name) | |
break | |
def text (self) : | |
return self.currentText () | |
def hasItem (self, item) : | |
return self.findText (item, Qt.MatchFixedString) != -1 | |
def getItems (self) : | |
items = [] | |
for i in xrange (self.count ()) : | |
items.append (self.itemText (i)) | |
return items | |
def getValues (self, role = None) : | |
values = [] | |
role = role or (Qt.UserRole + 1) | |
for i in xrange (self.count ()) : | |
values.append (self.itemData (i, role)) | |
return values | |
def setItems (self, items, current = None) : | |
model, current = listModel (items, current) | |
self.setModel (model) | |
self.setCurrentIndex (current) | |
def comboBox (items, handler, current = None) : | |
w = CustomComboBox (items, current = current) | |
w.currentChanged.connect (handler) | |
return w | |
def onchange(what): | |
logger.debug("What? %s" % str(what)) | |
def main(): | |
app = QApplication([]) | |
w = comboBox(["1", "2", "3"], onchange, "2") | |
logger.debug("getItems: %s" % str (w.getItems())) | |
logger.debug("getValues: %s" % str(w.getValues())) | |
logger.debug("current: %s" % str(w.current())) | |
logger.debug("setCurrent: %s" % str(w.setCurrent("3"))) | |
assert w.current() == "3" | |
w.append("4") | |
logger.debug("getItems: %s" % str(w.getItems())) | |
w.setItems(["5", "6", "7", "8"], current="8") | |
assert w.hasItem("8") | |
assert w.text() == "8", w.text() | |
w.remove(("8", "8")) | |
w.show() | |
app.exec_() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment