Is there any simple way to make some settings change at PyQT Designer for QTableWidget on ui form that I'll become able to copy from the table with the simple selections (extended selection mode) and Ctrl+C standard copy procedure.
By default even with multi-selection after Ctrl+C it copying to clipboard only last selected value, which is not good.
Quick sample code:
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import sys
data = {'col1': ['1', '2', '3', '4'],
'col2': ['1', '2', '1', '3'],
'col3': ['1', '1', '2', '1']}
class TableView(QTableWidget):
def __init__(self, data, *args):
QTableWidget.__init__(self, *args)
self.data = data
self.setData()
self.resizeColumnsToContents()
self.resizeRowsToContents()
def setData(self):
horHeaders = []
for n, key in enumerate(sorted(self.data.keys())):
horHeaders.append(key)
for m, item in enumerate(self.data[key]):
newitem = QTableWidgetItem(item)
self.setItem(m, n, newitem)
self.setHorizontalHeaderLabels(horHeaders)
# def keyPressEvent(self, event):
# clipboard = QApplication.clipboard()
# if event.matches(QKeySequence.Copy):
# print('Ctrl + C')
# clipboard.setText("some text")
# if event.matches(QKeySequence.Paste):
# print(clipboard.text())
# print('Ctrl + V')
# QTableView.keyPressEvent(self, event)
def main(args):
app = QApplication(args)
table = TableView(data, 4, 3)
table.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
I have seen many kinda similar questions (Here, Or here, Or here, Or even here) but all of those answers are so clumsy and I don't believe that for such simple operation I need to implement another class with slots. I hope that somebody knows more elegante way, make it as easy as it implemented by default at lineEdit widget.