简体   繁体   English

单击时发出 QTableWidgetItem 特定信号

[英]Emiting QTableWidgetItem specific signal on being clicked

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.我想实现可以选择和取消选择 QTableWidget 内的多个 QTableWidgetItems 的功能,以便知道这些 QTableWidgetItems 的值和“位置”以供进一步使用。

Inside of the QTableWidget there are some empty cells, for aesthetics/spacing, which should be ignored by the functionality I'm trying to implement. QTableWidget 内部有一些空单元格,用于美观/间距,我试图实现的功能应该忽略它们。 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?问题:如何实现在按下特定 QTableWidgetItem 时发出特定信号的功能?


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:示例 ui 文件:

<?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.应用程序的用户应该能够选择多个 QTableWidgetItem,以便项目所在行的信息可以传输到另一个 QTableWidget 以供进一步使用。

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.如果元素存在,则itemClicked信号确实传递被按下的元素,默认情况下,Qt Designer 仅创建它编辑过的元素,在适当的地方放置文本。

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM