Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. use "Control + C" to copy an item from QTableWidget , but get some data in database
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zac15
    wrote on last edited by Zac15
    #1

    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.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 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Z 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        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.

        Z Offline
        Z Offline
        Zac15
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Z 1 Reply Last reply
          0
          • SGaistS SGaist

            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 ?

            Z Offline
            Z Offline
            Zac15
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Nice !

              Thanks for sharing your findings :-)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved