Hi Olivier,
I don't know if you managed to solve this already but if not I think the following example should do what you need. I wrote it for a project of my own with similar requirements and uses a custom QWidget with mouse events rather than a table or checkboxes. You can use the value() and setValue() functions to get and set the value programatically or click on the individual bits to toggle there values. Hope you find it useful, here goes:
@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class BitWidget(QWidget):
def init(self, num_of_bits, parent=None):
QWidget.init(self, parent)
self.num_of_bits=num_of_bits
self.data=[0 for b in range(num_of_bits)]
self.data[1]=1
self.box_size=30
self.font=QFont('Serif', 12, QFont.Light)
def setBoxSize(self, size): self.box_size=size
def setFont(self, font): self.font=font
def sizeHint(self): return QSize(self.num_of_bits*self.box_size, self.box_size)
def paintEvent(self, event):
painter=QPainter()
painter.begin(self)
painter.setFont(self.font)
size=self.size()
width=size.width()-1
height=size.height()-1
for i,v in enumerate(self.data):
painter.fillRect(i*self.box_size, 0, self.box_size, self.box_size, Qt.green if self.data[i] else Qt.red)
painter.drawLine(i*self.box_size, 0, i*self.box_size, height)
valueString="%d" % i
fontWidth=painter.fontMetrics().width(valueString)
fontHeight=painter.fontMetrics().height()
painter.drawText((i*self.box_size)-(fontWidth/2)+(self.box_size/2),
(self.box_size/2)+(fontHeight/2),
valueString)
painter.setPen(Qt.black)
painter.drawRect(0, 0, width, height)
painter.end()
def mousePressEvent(self, event):
b=event.pos().x()//self.box_size
self.data[b]^=1
self.repaint()
event.accept()
def value(self):
value=0
mask=1
for i,v in enumerate(self.data):
if v: value|=mask
mask<<=1
return value
def setValue(self, value):
mask=1
for i,v in enumerate(self.data):
self.data[i]=(value&mask)>>i
mask<<=1
self.repaint()
class Widget(QWidget):
def init(self, parent=None):
QWidget.init(self, parent)
QVBoxLayout(self)
self.bitWidget=BitWidget(16, self)
self.layout().addWidget(self.bitWidget)
valueButton=QPushButton("Value", self)
valueButton.clicked.connect(self.printValue)
self.layout().addWidget(valueButton)
self.valueSpinBox=QSpinBox(self)
self.valueSpinBox.setRange(0, 2**16)
self.layout().addWidget(self.valueSpinBox)
setValueButton=QPushButton("Set Value", self)
setValueButton.clicked.connect(self.setValue)
self.layout().addWidget(setValueButton)
def printValue(self): print "Value:", self.bitWidget.value()
def setValue(self): self.bitWidget.setValue(self.valueSpinBox.value())
if name=="main":
from sys import argv, exit
a=QApplication(argv)
w=Widget()
w.show()
exit(a.exec_())
@
It should look something like this:
!http://s15.postimage.org/r3nwtauor/bitwidget.png(BitWidget Screenshot)!
Feel free to ask any questions, hope it helps.
Rob