gpt4 book ai didi

python - 单击时发出 QTableWidgetItem 特定信号

转载 作者:行者123 更新时间:2023-12-01 07:32:18 25 4
gpt4 key购买 nike

我想实现可以选择和取消选择 QTableWidget 内的多个 QTableWidgetItem 的功能,以便了解这些 QTableWidgetItem 的值和“位置”以供进一步使用。

QTableWidget 内部有一些空单元格,用于美观/间距,我尝试实现的功能应忽略这些空单元格。但这也可以通过检查所选项目是否有内容轻松完成。

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

这将是最合适的信号,但我无法指定或知道在表格小部件内单击了哪个项目,或者是否选择了或未选择该项目。

问题:如何实现在按下特定 QTableWidgetItem 时发出特定信号的功能?

<小时/>

这里有一些用于示例的伪代码:

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)


示例 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>

<小时/>

我对如何轻松实现此功能感到非常困惑,并且愿意接受有关不同方法的建议。

应用程序的用户应该能够选择多个 QTableWidgetItem,以便可以将项目所在行的信息传输到另一个 QTableWidget 以供进一步使用。

最佳答案

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

关于python - 单击时发出 QTableWidgetItem 特定信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57158840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com