当前位置:网站首页>[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 ,
边栏推荐
- Schnuka: working principle of robot visual grasping machine visual grasping
- A simple example of delegate usage
- Mysql的json格式查询
- Unity script visualization about layout code
- 软考信息处理技术员有哪些备考资料与方法?
- Ffmpeg record a video command from RTSP
- Monai version has been updated to 0.9. See what new functions it has
- 想考中级软考,一般需要多少复习时间?
- Laya common script commands
- 在线硬核工具
猜你喜欢

Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)

Deeply understand the characteristics of database transaction isolation

软考一般什么时候出成绩呢?在线蹬?

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

JSON format query of MySQL

Deep understanding of Apache Hudi asynchronous indexing mechanism

Socket communication principle and Practice

Static semantic check of clang tidy in cicd

Basic introduction of yarn and job submission process

想考中级软考,一般需要多少复习时间?
随机推荐
CSAPP Bomb Lab 解析
VR development optimization
The seventh training assignment
[recommendation system 01] rechub
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
2021-04-08
Cluster task scheduling system lsf/sge/slurm/pbs based on HPC scenario
[pro test feasible] error while loading shared libraries solution
Typescript interface inheritance
请问申购新股哪个证券公司开户是最好最安全的
【亲测可行】error while loading shared libraries的解决方案
Schnuka: working principle of robot visual grasping machine visual grasping
Those confusing concepts (3): function and class
IDEA快捷键大全
[OneNote] can't connect to the network and can't sync the problem
南航 PA3.1
Deep understanding of Apache Hudi asynchronous indexing mechanism
555 circuit details
PHP \ newline cannot be output
The gun startles the dragon, and the crowd "locks" Zhou Zhi
