当前位置:网站首页>[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 eighth training assignment
- 1323: [example 6.5] activity selection
- 2021-04-23
- The sixth training assignment
- IDEA快捷键大全
- Records on the use of easyflash v3.3
- 软考一般什么时候出成绩呢?在线蹬?
- How to successfully pass the senior system architecture designer in the second half of the year?
- Typescript interface inheritance
猜你喜欢
软考中级有用吗??
[recommendation system 01] rechub
The gun startles the dragon, and the crowd "locks" Zhou Zhi
JSON format query of MySQL
Unable to open kernel device '\.\vmcidev\vmx': operation completed successfully. Reboot after installing vmware workstation? Module "devicepoweron" failed to start. Failed to start the virtual machine
Socket communication principle and Practice
2022年上半年5月网络工程师试题及答案
2021-04-08
Find the greatest common divisor and the least common multiple (C language)
What are the test preparation materials and methods for soft exam information processing technicians?
随机推荐
深入理解Apache Hudi异步索引机制
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
Galaxy Kirin desktop operating system installation postgresql13 (source code installation)
打算参加安全方面工作,信息安全工程师怎么样,软考考试需要怎么准备?
Mendeley -- a free document management tool that automatically inserts references into papers
A simple example of delegate usage
Ping tool ICMP message learning
Socket communication principle and Practice
shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
Schnuka: machine vision positioning technology machine vision positioning principle
[pytorch 07] hands on deep learning chapter_ Preliminaries/ndarray exercises hands-on version
[STM32] actual combat 3.1 - drive 42 stepper motors with STM32 and tb6600 drivers (I)
Compile QT project script with qmake
2022年7月10日“五心公益”活动通知+报名入口(二维码)
Schnuka: working principle of robot visual grasping machine visual grasping
TypeScript 接口继承
JS implementation chain call
Online hard core tools
CAS mechanism
Ffmpeg record a video command from RTSP