PDA

View Full Version : QTableView Hidden Sections and Indexes



mechsin
11th July 2012, 22:59
I am using a QTableView. In my view I have given the user the ablility to hide columns and unhide them through a context menu and also to move the columns. I also wanted to give the user the abliltiy to copy and paste between excel and my table view. The copy part I have down pasting is becoming a problem. In my case let say in excel I copy a 2x2 block of cells to the clipboard. I then select a cell in my table view that belongs to a column that has been moved and had a hidden column next to it. So from selecting that one cell I expect it to paste the 2x2 block on the clipboard. However I am getting confused in the tangle between switching back and forth from the logical index to the visual index and my problem is further complicated by the fact that the hidden columns are counted in the visual index. So instead of incrementing my column index by one I have to check if the next column is hidden or not and it just seems like to much I must be doing something wrong. Is there a way to get a visual index without the hidden columns in it. I am using PyQt. A sample of something that kind of works is below.
getlistfromclipboard is returning a list of list which I am iterating through.

sel = self.selectedIndexes()

lidx = sel[0]
if len(sel) == 1:
buf = self.getlistfromclipboard()
hh = self.horizontalHeader()
rz = sel[0].row()
log_col = sel[0].column()
vis_col = hh.visualIndex(log_col)
for row_num, row in enumerate(buf):
row_num = row_num + rz
for col_num, value in enumerate(row):
while True:
lcol = hh.logicalIndex(col_num + vcz)
if (self.horizontalHeader().isSectionHidden(lcol) or
(col_num + vcz) <= self.model().columnCount()):
print(col_num + vcz)
col_num = col_num + 1
else:
idx = self.model().createIndex(row_num, lcol)
break
self.model().setData(idx, QtCore.QVariant(value))

wysota
15th July 2012, 18:49
Visual index is just the index of the column that takes into account reordering of columns. It does not carry any information about hidden state. Thus you need to take both visualIndex() and isSectionHidden() into consideration.