c++ - HorizontalHeader with QCheckBox in cell 0 -
how add horizontalheader checkbox in first index (cell 0) qtablewidget in qt? below instructions did not work.
headeritem->setflags(headeritem->flags() ^ qt::itemiseditable); headeritem->data(qt::checkstaterole); headeritem->setcheckstate(qt::checked); qtablewidget->sethorizontalheaderitem(0,headeritem);
please share thoughts
you can set flag, see documentation:
headeritem->setflags(headeritem->flags() ^ qt::itemiseditable | qt::itemisusercheckable);
that first thought. after several tries , research in documentation , several fora quite sure, items in qheaderview don't support checkboxes.
but can set different icons headeritem in dependance of (pseudo-)checkstate , save checkstate if necassary in headeritem.data(userrole)
. did in following steps (i know python, think, can translate c++):
set icon:
headeritem.seticon(qtgui.qicon('unchecked.png')) headeritem.setdata(256,'unchecked') headeritem.setflags(qtcore.qt.itemisenabled|qtcore.qt.itemisselectable)
connect signal
self.horizontalheader().sectionclicked.connect(self.changeicon)
and define method change icons
def changeicon(self,index): if self.horizontalheaderitem(index).data(256) == 'unchecked': self.horizontalheaderitem(index).seticon(qtgui.qicon('checked.png')) self.horizontalheaderitem(index).setdata(256,'checked') else: self.horizontalheaderitem(index).seticon(qtgui.qicon('unchecked.png')) self.horizontalheaderitem(index).setdata(256,'unchecked')
that's result:
Comments
Post a Comment