python - Inserting multiple QCheckBox into QTableWidget odd rows -
i'm trying create table 160 rows, , inserting qcheckbox
every odd number of rows, on column 10. problem is, have create 80 qcheckbox
(one each row, can separately assigned user)...
creating 1 one 80 qcheckbox
objects 9 projects have nonsense!
is there way of doing loop? can't think of anything, searched answer , found nothing.
[...] # importing pyside pyside import qtgui, qtcore [...] # creating table class table(qtgui.qdialog): def __init__(self, parent=none): super(table, self).__init__(parent) self.table = qtgui.qtablewidget() self.table.setrowcount(160) self.table.setcolumncount(10) # tricky part: chkboxitem = qtgui.qtablewidgetitem() chkboxitem.setflags(qtcore.qt.itemisusercheckable|qtcore.qt.itemisenabled) chkboxitem.setcheckstate(qtcore.qt.unchecked) chkboxitem2 = qtgui.qtablewidgetitem() chkboxitem2.setflags(qtcore.qt.itemisusercheckable|qtcore.qt.itemisenabled) chkboxitem2.setcheckstate(qtcore.qt.unchecked) chkboxitem3 = qtgui.qtablewidgetitem() chkboxitem3.setflags(qtcore.qt.itemisusercheckable|qtcore.qt.itemisenabled) chkboxitem3.setcheckstate(qtcore.qt.unchecked) [...] # insert of them in table: self.table.setitem(0, 10, chkboxitem) self.table.setitem(2, 10, chkboxitem2) self.table.setitem(4, 10, chkboxitem3) self.table.setitem(6, 10, chkboxitem4) self.table.setitem(8, 10, chkboxitem5) self.table.setitem(10, 10, chkboxitem6) self.table.setitem(12, 10, chkboxitem7) [...]
this basic script creates ui containing 160*10 qtable , qpushbutton. every odd row, checkbox added in cell of 10th column. clicking on button displays list of state of checkboxes.
states:
- 0: unchecked
- 2: checked
- there state 1 don't remember used for, i'll check docs.
note:
this has been made using pyqt
code:
import math, sys pyqt4.qtcore import qt, qtimer pyqt4.qtgui import * class mainwindow(qmainwindow): def __init__(self, parent = none): qmainwindow.__init__(self, parent) #create basic ui self.mainwidget = qwidget(self) self.table = qtablewidget() self.table.setrowcount(160) self.table.setcolumncount(10) self.button = qpushbutton("print stuff") layout = qvboxlayout(self.mainwidget) layout.addwidget(self.table) layout.addwidget(self.button) self.setcentralwidget(self.mainwidget) self.button.clicked.connect(self.printstuff) ################# #fill table self.rowrange = range(0, self.table.rowcount(), 2) in self.rowrange: chkboxitem = qtablewidgetitem() chkboxitem.setflags(qt.itemisusercheckable|qt.itemisenabled) chkboxitem.setcheckstate(qt.unchecked) self.table.setitem(i, 9, chkboxitem) ############### def printstuff(self): #you can remove this, testing purpose print [(i+1, self.table.item(i, 9).checkstate()) in self.rowrange] if __name__ == "__main__": app = qapplication(sys.argv) window = mainwindow() window.show() sys.exit(app.exec_())
Comments
Post a Comment