当前位置:网站首页>[pyqt] the cellwidget in tablewidget uses signal and slot mechanism
[pyqt] the cellwidget in tablewidget uses signal and slot mechanism
2022-07-07 10:59:00 【__ Watson__】
demand
Need to be in tableWidget Inside cellWidget( For example, the QComboBox and QCheckBox) Respond to the operation of , This response will act on others cellWidget
before this , I wanted to pass QTableWidget Signal to achieve , But I tested it ,cellWidget The following signals cannot be sent , Unable to meet the above requirements .
Solution
- In the use of
setCellWIdget
When adding anonymous controls , Use.connect()
Connect the anonymous control object to the corresponding slot function ; - Because of adding cellWidget when , Control is an anonymous object , In order to facilitate subsequent recognition and operation , Use dictionary as registration , Bind the object to the row index ;
- In the slot function Use
self.sender()
, Get the control object that sends this signal to trigger this slot function , Then get the row of the control through the registration dictionary , Then get other control objects on the row from the registration dictionary of the number of rows for operation .
Code example
- Create an anonymous control registration dictionary
self.cellWidgetRegistry1: Typing.Dict[QComboBox, int] = dict()
self.cellWidgetRegistry2: Typing.Dict[int, QCheckBox] = dict()
- add to cellWidget Control , And connect the slot function , And register anonymous control objects
def update_tableWidget(self):
for row in range(12):
cbb = QComboBox() # Create anonymous control objects
cbb.addItems(["1", "2", "3"])
self.tableWIdget.setCellWidget(row, 1, cbb) # add to cellWidge
cbb.currentIndexChanged.connect(self.on_cbb_currentIndexChanged) # Anonymous control connection slot function
self.cellWidgetRegistry1[cbb] = row # Register to dictionary Bind objects and rows
ckb = QCheckBox()
self.tableWIdget.setCellWidget(row, 2, ckb) # add to cellWidge
self.cellWidgetRegistry2[row] = ckb # Register to dictionary Bind rows and objects
- Slot function gets the sender
@QtCore.pyqtSlot(int)
def on_cbb_currentIndexChanged(self, idx):
cbb = self.sender() # Get the sender of this signal ( The control object )
row = self.cellWidgetRegistry1.get(cbb, 0)
# By line number and another registration dictionary Find other controls in the row in reverse
cbb2 = self.cellWidgetRegistry2.get(row, QCheckBox)
...
Reference resources
【1】 pyqt Signal slot Judge the sender (sender)
【2】QTableWidget About China cellWidget Signal processing method
Other options
You can try to use setItemDelegate
agent ,
边栏推荐
- The seventh training assignment
- Arduino board description
- “梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
- BUUCTF---Reverse---reverse1
- Cmake learning manual
- 2022年上半年5月网络工程师试题及答案
- How to successfully pass the senior system architecture designer in the second half of the year?
- SQL Server knowledge gathering 9: modifying data
- 软考中级,软件设计师考试那些内容,考试大纲什么的?
- China Southern Airlines pa3.1
猜你喜欢
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
How to successfully pass the senior system architecture designer in the second half of the year?
Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
Wallhaven壁纸桌面版
2021-04-08
软考一般什么时候出成绩呢?在线蹬?
Mendeley -- a free document management tool that automatically inserts references into papers
软考中级,软件设计师考试那些内容,考试大纲什么的?
Static semantic check of clang tidy in cicd
【亲测可行】error while loading shared libraries的解决方案
随机推荐
單調性約束與反單調性約束的區別 monotonicity and anti-monotonicity constraint
JS implementation chain call
Unity determines whether the mouse clicks on the UI
Compile QT project script with qmake
IDEA快捷键大全
[machine learning 03] Lagrange multiplier method
Records on the use of easyflash v3.3
Laya common script commands
What is an intermediate network engineer? What is the main test and what is the use?
[système recommandé 01] rechub
无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
[recommendation system 01] rechub
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
Those confusing concepts (3): function and class
Unity script visualization about layout code
shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
Online hard core tools
【机器学习 03】拉格朗日乘子法
[actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
2022.7.6DAY598