Important: Please read the Qt Code of Conduct -

use "Control + C" to copy an item from QTableWidget , but get some data in database

  • Hi guys,

    When I used "Control+C" to copy a selected item from QTableWidget table, I didn't get the text of the item, but got some data from database which is related to the item.

    For example, I selected the highlighted item in the screenshot below and clicked Control+C. When pasting the text, I got data from a table regarding this item.

    Using PyQt5, I tested on both Windows and Mac. Same unexpected result.

    I am very very confused by this behaviour.

    Does anyone know why this happend and how to solve it. Furthermore, I'm interested in how the copy slot works? Thank you.

    Screen Shot 2020-10-29 at 11.21.43 pm.png

    My script:

            self.ui.widget = QtWidgets.QWidget()
            self.ui.layout_VArea = QtWidgets.QVBoxLayout(self.ui.widget)
            font = QtGui.QFont()
            # use QtableWidget to create table
            self.ui.tableWidget = QtWidgets.QTableWidget(self.ui.widget)
            labels = [self.labels[self.layer].get(field, field) for field in self.userFields[self.layer]]
            print(labels) # a list of string
            print(self.ui.tableWidget.selectionBehavior()) # 0 
            print(self.ui.tableWidget.selectionMode()) # 3
            # get how many rows in the values
            rows = len(values)
            # set table rows
            for indexRow, id in enumerate(values.keys()):
                rowString = list(map(str, values[id]))
                print(rowString) # a string list
                for indexColumn, item in enumerate(rowString):
                    flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
                    qItem = QtWidgets.QTableWidgetItem(item)
                    self.ui.tableWidget.setItem(indexRow, indexColumn, qItem)

  • Lifetime Qt Champion


    Please provide a complete runnable example.

    You are talking about database data yet there's no database related code in what you posted so it's impossible to tell what is going on. You are using a QTableWidget but there's no indication about the data origin nor if you process it before inserting it.

  • Thank you, @SGaist

    I process the data to be string before inserting into QTableWidget.

    When calling QTableWidgetItem, item is just a string at the moment. So I don't why Control + C query data from table.

    qItem = QtWidgets.QTableWidgetItem(item)

    My script is integrated with QGIS, so it's hard to provide a reproduce/runnable one. Really sorry for this.

    Is there a way to trouble shoot the issue or catch how PyQt5 handle copy action internally? I hope to catch the slot that deals with the Control+C event.

    Many thanks.

  • Lifetime Qt Champion

    Which version of PyQt are you using ?

    No slots involved and in any case it's down at the C++ level.

    Do you also have strange results if you create a sample script that just populates a QTableWidget ?

  • Hi @SGaist, the issue was fixed.
    The weird behaviour was caused by QGIS. Actually, there is a global shortcut for "Control+C" to copy the selected features on the map canvas.

    The table is joined to features on the map, so when I click an item on the form, a feature is also selected.
    Therefore, when clicking "Control+C", QGIS will query the whole feature's attributes instead of copying the single item.

    That's the reason why this issue happened.

    Thank you for your reply. Thanks a lot.

  • Lifetime Qt Champion

    Nice !

    Thanks for sharing your findings :-)

Log in to reply