当前位置:网站首页>[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
setCellWIdgetWhen 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 ,
边栏推荐
- SQL Server knowledge gathering 9: modifying data
- Long list performance optimization scheme memo
- Unity websocket client
- 2022年上半年5月网络工程师试题及答案
- Simple and easy to modify spring frame components
- 2021 summary and 2022 outlook
- Schnuka: machine vision positioning technology machine vision positioning principle
- Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario
- Laya common script commands
- 2022.7.6DAY598
猜你喜欢

IDEA快捷键大全
![1324: [example 6.6] integer interval](/img/5d/29db4d51ec7a2685f8aeffe5be68da.png)
1324: [example 6.6] integer interval

Multithreaded asynchronous orchestration

【STM32】实战3.1—用STM32与TB6600驱动器驱动42步进电机(一)

What are the contents of the intermediate soft test, the software designer test, and the test outline?

Mpx 插件

Find the greatest common divisor and the least common multiple (C language)

2022年上半年5月网络工程师试题及答案
![P1223 queuing for water /1319: [example 6.1] queuing for water](/img/09/29f19b32f0a3c82ddb3c30d5d0b16e.png)
P1223 queuing for water /1319: [example 6.1] queuing for water

uniCloud
随机推荐
IDEA快捷键大全
1323: [example 6.5] activity selection
PHP \ newline cannot be output
Rolling puddle Uni_ App (VIII)
变量的解构赋值
2022.7.5DAY597
2021 summary and 2022 outlook
Unity websocket client
[recommendation system 01] rechub
POJ1821 Fence 题解报告
Mpx 插件
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
ADB utility commands (network package, log, tuning related)
软考一般什么时候出成绩呢?在线蹬?
Cmake learning manual
[machine learning 03] Lagrange multiplier method
Deep understanding of Apache Hudi asynchronous indexing mechanism
"Dream Cup" 2017 Jiangsu information and future primary school summer camp it expert PK program design questions
SQL Server knowledge gathering 9: modifying data
Introduction to shell programming
