[SOLVED] QTableWidget Drop event get cell
-
I have implemented a Drop event for a QTableWidget, and i have 2 problems:
1 - The detection of the row fails when there is only one row, source.rowAT(position.y()) gives me -1 , if there is more than one row, at the same position (first row) get the correct number (1).
2- The text drop fails, when i drop a file it works correctly (the PATH is saved)
The code:
@
self.ZipTable.installEventFilter(self)
.........def eventFilter(self, source, event):
if (event.type() == QEvent.DragEnter):
if event.mimeData().hasUrls or event.mimeData().hasUrls is not None or event.mimeData().text() != '':
event.accept()
return True
else:
event.ignore()
return Falseif (event.type() == QEvent.Drop):
if event.mimeData().hasUrls:
for url in event.mimeData().urls():
item = QTableWidgetItem()
item.setText(str(url.toLocalFile()))position = event.pos() row = source.rowAt(position.y()) column = source.columnAt(position.x()) print row,column if row <= 0 or column <= 0 : self.AddRow(source, icon = eliminar_icono) source.setItem(source.rowCount()-1,column, item) else: source.setItem(row-1,column, item)
elif event.mimeData().text() != '':
item = QTableWidgetItem()
item.setText(event.mimeData().text())position = event.pos() row = source.rowAt(position.y()) column = source.columnAt(position.x()) print row,column if row <= 0 or column <= 0 : self.AddRow(source, icon = eliminar_icono) source.setItem(source.rowCount()-1,column, item) else: source.setItem(row-1,column, item)
return True
else:
return False
@ -
The rowAt() and columnAt() expect a point in table content coordinates. The event delivers a point in widget coordinates.
To get the right point you need to subtract the size of the table headers from the event pos. -
Solved.
New code:
@
self.ZipTable.installEventFilter(self)
.........def eventFilter(self, source, event):
if (event.type() == QEvent.DragEnter):
if event.mimeData().hasUrls or event.mimeData().hasUrls is not None or event.mimeData().text() != '':
event.accept()
return True
else:
event.ignore()
return Falseif (event.type() == QEvent.Drop):
if event.mimeData().hasUrls:
for url in event.mimeData().urls():
item = QTableWidgetItem()
item.setText(str(url.toLocalFile()))position = event.pos() row = source.rowAt(position.y()) column = source.columnAt(position.x()) print row,column if row <= 0 or column <= 0 : self.AddRow(source, icon = eliminar_icono) source.setItem(source.rowCount()-1,column, item) else: source.setItem(row-1,column, item)
elif event.mimeData().text() != '':
item = QTableWidgetItem()
item.setText(event.mimeData().text())position = event.pos() # 30 is the row Height row = source.rowAt(position.y() - 30) column = source.columnAt(position.x()) print row,column if row <= 0 or column <= 0 : self.AddRow(source, icon = eliminar_icono) source.setItem(source.rowCount()-1,column, item) else: source.setItem(row,column, item)
return True
else:
return False
@ -
Aww, don't hardcode the height. Headers can be resized or hidden entirely. Their size depends on contents, font size, dpi settings and system style applied. 30 is not an universal value. For example on my computer the default is different.
What you should do is check if there is a header at all with horizontalHeader(). Then check if the header is not hidden with isVisible(). Then take the height with height().
Apart from that you should apply the same logic to columns. The x coordinate should be adjusted too.