python - Cant get a custom ItemDelegate to work -
python - Cant get a custom ItemDelegate to work -
first off, im new python , pyqt please bear me.
im using qtableview qsqltablemodel works intended. lastly column of view contains 0 , 1 value want display checkbox , column should editable.
ive read should subclass qitemdelegate did. unluckily table wont show lastly column checkbox.
i tried set delegate lastly column (the way prefer) using setitemdelegateforcolumn(), didnt work. modified , set entire qtableview using setitemdelegate() reacting requests lastly column. still wont work. wont work means there no error messages wont ;) seems none of methods reimplemented gets ever called except init(). guess im missing fundamental.
ive extracted relevant lines of code, kscheckboxdelegate subclass. version delegate set entire qtableview.
-- code applications main class -- self.taglist = qtableview() self.tagmodel = qsqltablemodel() self.tagmodel.settable("data") self.tagmodel.select() self.taglist.setmodel(self.tagmodel) print self.taglist.itemdelegate() mydel = kscheckboxdelegate(self) mydel.colnumber = 3 self.taglist.setitemdelegate(mydel) -- kscheckboxdelegate.py -- pyqt4.qtgui import * class kscheckboxdelegate(qstyleditemdelegate): colnumber = none def __init__ (self, parent = none): print "kscheckboxdelegate::init" qstyleditemdelegate.__init__(self, parent) def createeditor(self, parent, option, index): print "kscheckboxdelegate::createeditor" if index.column()==self.colnumber: homecoming qcheckbox(self) else: homecoming qstyleditemdelegate.createeditor(self, parent, option, index) def seteditordata (self, editor, index): print "kscheckboxdelegate::seteditordata" if index.column() == self.colnumber: cont = index.model().data(index, qt.displayrole).tostring() if cont == "1": editor.setcheckstate(qt.checked) else: editor.setcheckstate(qt.unchecked) else: qstyleditemdelegate.seteditordata(self,editor, index) def setmodeldata (self, editor, model, index): print "kscheckboxdelegate::setmodeldata" if index.column() == self.colnumber: if editor.checkbox.checkstate() == qt.checked: model.setdata(index, 1) else: model.setdata(index, 0) else: qstyleditemdelegate.setmodeldata(self, editor, model, index)
any hints me on issue?
furthermore have difficulties currentchanged() signal of qtableviews selectionmodel. im printing top right coordinates of selection. maintain getting wrong indexes (not invalid) when clicking left mouse button. using cursor keys gets me right indexes. using selectionchanged() has same behaviour. im getting coordinates of sec lastly selected cell of qtableview. instance im clicking on coordinates <1,1> <2,1> sec click show me coordinates <1,1>.
selind = self.taglist.selectionmodel().selectedindexes() if(len(selind) > 0): self.xposdata=selind[0].column() self.yposdata=selind[0].row()
fixed myself, using qtableview.currentindex() instead of selectionmodel.selectedindexes() :)
and lastly off using onmanualsubmit editstrategy doesnt throw error (return false) when calling submitall() doesnt save info either. works choosing onfieldchange editstrategy. can live not have intended do.
thanks time.
horst
i think simpler create own model basing qsqltablemodel, , 0/1 column homecoming qvariant() qdisplayrole , homecoming qt::checked/qt::unchecked qt::checkstaterole depending on value. other cases homecoming qsqltablemodel::data
class mysqltablemodel: public qsqltablemodel { public: // contructors qvariant data(const qmodelindex & index, int role = qt::displayrole) { if(index.column() == 3 /* 0/1 column */) { if(role == qt::displayrole) { homecoming qvariant(); } else if(role == qt::checkstaterole) { qstring value = qsqltablemodel::data(index, qt::displayrole).tostring(); homecoming value == "1" ? qt::checked : qt::unchecked; } } homecoming qsqltablemodel::data(index, role); } };
i know it's c++ code, logic still same, readjust python
python qt qt4 pyqt pyqt4
Comments
Post a Comment