qt - How can I select cells in a QTableView like characters in a QTextEdit? -
i'd build hex editor-like view using qtableview. each cell representation of byte of data. how can configure qtableview selection behaviour such acts typical text edit control? is, rather selecting rectangular region of cells, should select remaining cells on line, entire contents of intermediate lines, , partial contents on final line.
in diagram form (x selected, . unselected), want this:
.................. ......xxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxx xxxxxxxxxxxx...... .................. i not want this:
.................. ......xxxxxx...... ......xxxxxx...... ......xxxxxx...... ......xxxxxx...... ..................
ultimately, had success changing default behaviour of qitemselectionmodel subclassing , overriding select method, described here: https://stackoverflow.com/a/10920294/87207
here's key part of code:
class rollingitemselectionmodel(qitemselectionmodel): def qindex2index(self, index): """ convert qmodelindex offset buffer """ m = self.model() return (m.columncount() * index.row()) + index.column() def index2qindex(self, index): """ convert offset buffer qmodelindex """ m = self.model() r = index // m.columncount() c = index % m.columncount() return m.index(r, c) def select(self, selection, selectionflags): # pyqt5 doesn't have method overloading, type switch if isinstance(selection, qitemselection): # overload qitemselection passed arg 0 qindexes = selection.indexes() indices = [] qindex in qindexes: indices.append(self.qindex2index(qindex)) if indices: low = min(indices) high = max(indices) selection = qitemselection() in xrange(low, high): qi = self.index2qindex(i) # inefficient, functional # manually add bytes select, one-by-one selection.select(qi, qi) elif isinstance(selection, qmodelindex): # overload qmodelindex passed arg 0 # since it's single index, works expected. pass else: # in case raise runtimeerror("unexpected type arg 0: '%s'" % type(selection)) # fall through. select normal super(rollingitemselectionmodel, self).select(selection, selectionflags)
Comments
Post a Comment