1

I am using PyQGIS to develop a table as a QGIS 3.4 plugin.

The table is created by QTableWidget and looks good.

The weird thing is when I try to copy (Ctrl+C keyboard shortcut) an item in the table, it indeed copies the whole feature, like Copy Feature action in the Identify Features tool.

Basically, the item is just a simple string. I really don't know why this behaviour happened.

e.g. in the screenshot below, I only copied the blue highlighted item. But when pasting, I got an output of all attributes of the corresponding feature. I didn't implement any function related to the Copy functionality.

enter image description here

Same result as I can get from Copy Feature action.

enter image description here

My Script:

    self.ui.scrollArea.setWidgetResizable(True)
    self.ui.widget = QtWidgets.QWidget()
    self.ui.scrollArea.setWidget(self.ui.widget)
    self.ui.layout_VArea = QtWidgets.QVBoxLayout(self.ui.widget)
    font = QtGui.QFont()
    font.setBold(True)
    font.setItalic(True)
    font.setPointSize(9)

    # use QtableWidget to create table
    self.ui.tableWidget = QtWidgets.QTableWidget(self.ui.widget)
    self.ui.tableWidget.setObjectName("tableWidget")
    self.ui.layout_VArea.addWidget(self.ui.tableWidget)
    self.ui.tableWidget.setColumnCount(len(self.userFields[self.layer]))

    labels = [self.labels[self.layer].get(field, field) for field in self.userFields[self.layer]]
    print(labels) # a list of string
        
    self.ui.tableWidget.setHorizontalHeaderLabels(labels)
    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
    self.ui.tableWidget.setRowCount(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)
            qItem.setFlags(flags)
            self.ui.tableWidget.setItem(indexRow, indexColumn, qItem)
1

After looking into the issue for some time, I finally found out why this happend.

Explain:

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.

Solution:

You can disable 'Copy Feature' shortcut(Ctrl+C).

In QGIS, Setting -> Keyboard Shortcuts -> Copy Feature -> Set None

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.