Pyqt: Qtableview + Qsqltablemodel - Copy And Paste All Selected Rows Or Columns Into Notepad Or Excel
To start, I've already looked at ekhumoro's code on a nearly similar subject Here. However, when I attempt to implement this code, I am getting a different result. Instead of copyi
Solution 1:
Your implementation is broken in several ways, so the code you copied never gets executed. As a result, the table's built-in copy function is used, which does not handle multiple selected items.
My original code for copying multiple table items is here. I think you should be able to get your example working by changing the following lines:
from PyQt4 import QtCore, QtGui, QtSql
# fix importsimport sys, io
...
classTableEditor(QtGui.QMainWindow, Search.Search_MainWindow):
def__init__(self, tableName, parent=None):
# do not ever use self.__class__ heresuper(TableEditor, self).__init__()
...
# install event-filter properly
self.tableView.installEventFilter(self)
defeventFilter(self, source, event):
...
# call super properlyreturnsuper(TableEditor, self).eventFilter(source, event)
Obviously, I cannot run your actual example code, so there may be other errors that I haven't spotted.
Solution 2:
This is much faster :
def copySelection(self):
clipboardString = StringIO()
selectedIndexes = self.ui.tableView.selectedIndexes()
if selectedIndexes:
countList = len(selectedIndexes)
for r in range(countList):
current = selectedIndexes[r]
displayText = current.data(QtCore.Qt.DisplayRole)
if r+1 < countList:
next_ = selectedIndexes[r+1]
if next_.row() != current.row():
displayText += ("\n")
else:
displayText += ("\t")
clipboardString.write(displayText)
pyperclip.copy(clipboardString.getvalue())
Post a Comment for "Pyqt: Qtableview + Qsqltablemodel - Copy And Paste All Selected Rows Or Columns Into Notepad Or Excel"