Emiting QTableWidgetItem specific signal on being clicked

China☆狼群 提交于 2021-02-17 02:52:06

问题


I want to implement the functionality that multiple QTableWidgetItems inside of a QTableWidget can be selected and unselected so that the values and "positions" of those QTableWidgetItems is known for further use.

Inside of the QTableWidget there are some empty cells, for aesthetics/spacing, which should be ignored by the functionality I'm trying to implement. But that could also easily be done afterwards by checking if a selected item does have content.

self.table_widget.itemClicked.connect(self.some_function)

Would be the signal best fitting but I cannot specify or know which item was clicked inside of the table widget or whether is was selected or unselected.

Question: How do I implement the functionality to emit a specific signal when a specific QTableWidgetItem is pressed?


Here some pseude code for use in examples:

class Window(QtWidgets.QMainWindow):
    def __init__(self):

        super(Window, self).__init__()
        uic.loadUi("qtdesignerfile.ui", self)
        self.show()
        #connect signal to function
        self.table_widget.itemClicked.connect(self.some_function)
        #create "global" list to save all selected items
        self.selected_items = []

    #some function to execute the logic
    def some_function(self):
        #condition distinguishing if item was just selected or unselected
        if QTableWidgetItem(row, column) is not already selected:

            #change background color to indicate selection
            self.table_widget.item(row,column).setBackground(QtGui.QColor(100,100,100))
            #"format" information of clicked item for further use
            selected_item = [str(table_widget.item(row,column).text()), row, column]
            #"save" selected item in global list for further use
            self.selected_items.append(selected_item)

        #condition distinguishing if item was just selected or unselected
        if QTableWidgetItem(row, column) is already selected:

            #change background color to indicate un-selection 
            self.table_widget.item(row,column).setBackground(QtGui.QColor(0,0,0))
            #"format" information of clicked item for further use
            selected_item = [str(table_widget.item(row,column).text()), row, column]
            #remove the un-selected item from global list
            self.selected_items.remove(selected_item)


Example ui-file:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>418</width>
    <height>297</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QTableWidget" name="table_widget">
      <row>
       <property name="text">
        <string>Row 1</string>
       </property>
      </row>
      <row>
       <property name="text">
        <string>Row 2</string>
       </property>
      </row>
      <row>
       <property name="text">
        <string>Row 3</string>
       </property>
      </row>
      <row>
       <property name="text">
        <string>Row 4</string>
       </property>
      </row>
      <row>
       <property name="text">
        <string>Row 5</string>
       </property>
      </row>
      <row>
       <property name="text">
        <string>Row 6</string>
       </property>
      </row>
      <column>
       <property name="text">
        <string>Grouping</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Column 2</string>
       </property>
      </column>
      <column>
       <property name="text">
        <string>Column 3</string>
       </property>
      </column>
      <item row="0" column="0">
       <property name="text">
        <string>category 1</string>
       </property>
      </item>
      <item row="1" column="1">
       <property name="text">
        <string>Entry (1,1)</string>
       </property>
      </item>
      <item row="1" column="2">
       <property name="text">
        <string>Entry(1,2)</string>
       </property>
      </item>
      <item row="2" column="1">
       <property name="text">
        <string>Entry(2,1)</string>
       </property>
      </item>
      <item row="2" column="2">
       <property name="text">
        <string>Entry(2,2)</string>
       </property>
      </item>
      <item row="3" column="0">
       <property name="text">
        <string>category 2</string>
       </property>
      </item>
      <item row="4" column="1">
       <property name="text">
        <string>Entry(4,1)</string>
       </property>
      </item>
      <item row="4" column="2">
       <property name="text">
        <string>Entry(4,2)</string>
       </property>
      </item>
      <item row="5" column="1">
       <property name="text">
        <string>Entry(5,1)</string>
       </property>
      </item>
      <item row="5" column="2">
       <property name="text">
        <string>Entry(5,2)</string>
       </property>
      </item>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>418</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>


I am really puzzled on how to easily implement this functionality and am open to suggestion on a different approach.

The user of the application should be able to select multiple QTableWidgetItems so that the information of the row the item is inside can be transferred to another QTableWidget for further use.


回答1:


The itemClicked signal does pass the element that is pressed if the element exists, by default Qt Designer only creates elements in which it edited, where appropriate where it placed text.

from PyQt5 import QtCore, QtGui, QtWidgets, uic

SelectedRole = QtCore.Qt.UserRole + 1000


class Window(QtWidgets.QMainWindow):
    def __init__(self):

        super(Window, self).__init__()
        uic.loadUi("qtdesignerfile.ui", self)

        # self.tableWidget.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.tableWidget.itemClicked.connect(self.onClicked)

    @QtCore.pyqtSlot(QtWidgets.QTableWidgetItem)
    def onClicked(self, it):
        state = not it.data(SelectedRole)
        it.setData(SelectedRole, state)
        it.setBackground(
            QtGui.QColor(100, 100, 100) if state else QtGui.QColor(0, 0, 0)
        )


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())


来源:https://stackoverflow.com/questions/57158840/emiting-qtablewidgetitem-specific-signal-on-being-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!