0

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.

enter image description here

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Nick PV
  • 415
  • 6
  • 14
  • that's why I said there "don't be rude" ... However, I meant simple for implementation, in a few lines of code or widget settings change at QtDesigner "more elegant way" and here (https://stackoverflow.com/questions/40469607/how-to-copy-paste-multiple-items-form-qtableview-created-by-qstandarditemmodel) it's implemented for table View not for tableWidget – Nick PV Sep 04 '19 at 23:42
  • 1) Place irrelevant information such as; "Hello", "Thanks", "Don't be rude", etc. in a question is deleted in an edition because it does not write anything in your post 2) I have not been rude, and if you consider that someone has been including me Then report it to a moderator and if he considers that if he went rude with you then that person will be sanctioned, so are the SO rules, you have been here for 2 years and you should know them. – eyllanesc Sep 04 '19 at 23:50
  • 3) The code you provide in your question has nothing to do with Qt Designer, 4) A part of your code that is commented already detects Ctrl + C and Ctrl + V so you just have to place the copySelection method in your class and call it when appropriate by changing only `self.tableView` to `self` – eyllanesc Sep 04 '19 at 23:52
  • 5) If your question would show an attempt and where you would clearly indicate where you are stuck then in that case it would be a different question, but in your current case it is duplicated of what I indicated. – eyllanesc Sep 04 '19 at 23:53
  • 6) QTableWidget inherits from QTableView so that any solution of QTableView is also a solution to QTableWidgets, the opposite is not valid. – eyllanesc Sep 04 '19 at 23:55
  • 7) Qt does not implement multicopy natively, so it’s your job to implement it. – eyllanesc Sep 05 '19 at 00:02
  • Then sorry me, just after 2 years I used to that that this community is very emotional. 3) That code was just a sample (for reference) 5) I'm asking for alternative way, because I don't know how to integrate that specific solution for my project 6) I have entire program where I'm using tablewidget, so I don't want to replace it with tableview 7) I did not know that there no multicopy natively, (this is part of the question as well), I expected that there some option to make it work in (select - copy) way like textbox. – Nick PV Sep 05 '19 at 00:24
  • So your question should be: *I have this code where I have tried to do what this publication X, Y and Z points out and I am stuck in A, B and C, what should I modify to make it work?, etc*, ask for a solution alternative is unproductive since the solution already exists, your problem is not that there is no solution but that in your particular code you do not know how to implement it. I recommend you take the time to edit your question. That you do not know how to implement something does not imply asking for another alternative solution. – eyllanesc Sep 05 '19 at 00:30
  • Your alternative solution request is like saying: "I don't know how to put a wheel to my car, someone could give me something else (stone, ball, etc.) to place it instead of the wheel" – eyllanesc Sep 05 '19 at 00:32
  • Not all the questions just with one answer, sometimes second or third answer here on the stack may be better than the most liked one. It happens to me a million times. And I'm asking not for a wheel or ball but I'm asking for more suitable by shape and color pice of cotton to tailor the hole in a blanket. and that piece which is on the table just not suitable, simple as that. Sometimes somebody can simply shoot with a brilliant solution which is easy to modify for whoever needs. – Nick PV Sep 05 '19 at 00:58
  • Well, there is only what you see, there is no magic solution unfortunately. – eyllanesc Sep 05 '19 at 01:00
  • Hope is still in my heart. I'm coming here to ask as for the last chance and last-resort after I googled everything, No luck this time, unfortunately. – Nick PV Sep 05 '19 at 01:14

0 Answers0