python - Get the index of a QTableWidget row knowing its elements -
[i'm using pyqt4, think qt4 issue not python specific.]
i have qtablewidget
. in each row, first column holds button. when clicked, row removed.
to remove row, use removerow(int row)
method, takes argument index of row. when connecting signal, can't know index of row because might change in meantime (for instance if first row removed, row indexes changed).
the accepted answer here suggests pass callback instance of qtablewidgetitem in line, row number item @ deletion time.
this nice, except none of elements of row qtablewidgetitem
. elements button , few comboboxes.
i can't figure out way around this.
can somehow fit 1 of elements qtablewidgetitem? should add qtablewidgetitem in sort of hidden column?
our current implementation uses indexat(qtgui.qapp.focuswidget())
(see other answer question mentioned above), looks sorry workaround me.
if replace button checkable qtablewidgetitem this
rm_item = qtgui.qtablewidgetitem() rm_item.setflags(qtcore.qt.itemisusercheckable | qtcore.qt.itemisenabled)
i have qtablewidgetitem can use row index. don't know how catch "checked" or "clicked" event button. found itemclicked
signal of qtablewidget
, i'd have filter other widgets out.
there has obvious i'm missing.
edit
from read here, add both qtablewidgetitem setitem , button widget setcellwidget same cell. doesn't seem natural me, apparently works (can't test right now).
i guess i'll that. add button, plus dummy qtablewidgetitem on same cell pass reference row.
is how meant be?
edit 2
or maybe qtablewidget
not proper widget , should using layout
, suggested here.
it seems using layout rather table possibly "correct" answer, may come it's own difficulties, seen in answer question:
if want continue using table, cleaner solution adding dummy items use persistent model index:
button = qtgui.qpushbutton(text, self.table) self.table.setcellwidget(row, column, button) index = qtcore.qpersistentmodelindex( self.table.model().index(row, column)) button.clicked.connect( lambda *args, index=index: self.handlebutton(index)) def handlebutton(self, index): print('button clicked:', index.row()) if index.isvalid(): self.table.removerow(index.row())
Comments
Post a Comment