Open PuchatekwSzortach opened 7 years ago
So, are you saying the documentation is wrong in that regard and the examples should be something like...
RedRectItem = items.RectItem # no ()
RedRectItem.setColor(Qt.red)
GreenRectItem = items.RectItem # no ()
GreenRectItem.setColor(Qt.green)
ITEMS = {
"rect" : RedRectItem,
"head" : GreenRectItem,
}
@nilsonholger Yes to lines 1 and 3, but I doubt that RedRectItem.setColor(Qt.red)
and GreenRectItem.setColor(Qt.green)
will work if RedRectItem
and GreenRectItem
are classes, not instances. How I implemented it in my code was by subclassing items.RectItem and making appropriate calls to self.setColor() in subclasses' constructors.
Of course, but could you please show me the snippet then as you would see it work? Thanks!
Here is my config.py for a very similar use-case:
from PyQt4.QtGui import QPen
from PyQt4.Qt import Qt
import sloth.items
class GreenItem(sloth.items.RectItem):
def __init__(self, *args, **kwargs):
sloth.items.RectItem.__init__(self, *args, **kwargs)
self.setColor(Qt.green)
class RedItem(sloth.items.RectItem):
def __init__(self, *args, **kwargs):
sloth.items.RectItem.__init__(self, *args, **kwargs)
self.setColor(Qt.red)
LABELS = (
{"attributes": {"type": "rect",
"class": "Face",
},
"item": GreenItem,
"text": "Face"
},
{"attributes": {"type": "rect",
"class": "Not-Face",
},
"item": RedItem,
"text": "Not-Face"
},
)
Documentation states that
RectItem
and friends have a__call()__
method implemented so that one can use a modified RectItem instance as a value corresponding to "item" key in LABELS portion of config file. This is to allow use like in following snipped taken from documentation:However trying to put an instance of RectItem into "item" portion of LABELS causes an exception. Here's a relevant portion of a sample config:
And here's the error message that occurs when trying to add a label:
Using class, or
"item": sloth.items.RectItem
, rather than instance, or"item": sloth.items.RectItem()
works.I'm using Sloth installed from source from commit
10c9f59
.