PyQT5 动态添加的按钮和标签事件绑定问题
如下代码所示,动态添加的label绑定点击和鼠标悬停事件都不执行,请问各位大佬该怎么写动态添加的button添加点击事件怎么把自己的属性作为参数传进去,或者其他内容参数。
tkinter 做了一个版本可以这么写
self.content_btn = Button(
self.frame1, text=value, width=40, command=lambda arg=value: self.text_click(arg))
但是qt实在不会,求大佬指导一下{:5_92:}
# 初始化行元素
def init_rows(self):
index = 0
for i, value in enumerate(totalArray.items()):
tital_labels.append(QtWidgets.QLabel(self.gridLayoutWidget))
tital_labels.setObjectName("tital_label")
tital_labels.setText(value + ':')
tital_labels.setStyleSheet("background-color:gold")
tital_labels.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
tital_labels.linkHovered.connect(self.linkHovered) # 不执行
tital_labels.linkActivated.connect(self.linkClicked) # 不执行
self.gridLayout.addWidget(tital_labels, int(i / 2), i % 2 * 9, 1, 1)
# 行button
val_btns.append(QtWidgets.QPushButton(self.gridLayoutWidget))
val_btns.setObjectName("valButton")
val_btns.setText(value)
val_btns.setToolTip(value)
val_btns.setStyleSheet("background-color:LavenderBlush")
val_btns.clicked.connect(lambda: self.show_all_row(value))
self.gridLayout.addWidget(val_btns, int(i/2), int(i % 2*9 + 1), 1, 1)
index = i
right_btn = QtWidgets.QPushButton(self.gridLayoutWidget)
right_btn.setText("Next")
self.gridLayout.addWidget(right_btn, int(len(titalArr)/2 + 1), 4, 1, 1)
left_btn = QtWidgets.QPushButton(self.gridLayoutWidget)
left_btn.setText("Previous")
self.gridLayout.addWidget(left_btn, int(len(titalArr)/2 + 1), 2, 1, 1)
def show_all_row(self,value):
print('当鼠标滑过label2标签时,触发事件' + value)
def label_hover(self):
print('当鼠标滑过label2标签时,触发事件')
def linkHovered(self):
print('当鼠标滑过label2标签时,触发事件')
def linkClicked(self):
print('当鼠标单击label4标签时,触发事件') # -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import QtCore, QtWidgets, QtGui
'''
Pyqt 动态的添加控件
'''
class DynAddObject(QWidget):
def __init__(self, parent=None):
super(DynAddObject, self).__init__(parent)
addButton = QtWidgets.QPushButton(u"添加控件")
self.layout = QtWidgets.QGridLayout()
self.layout.addWidget(addButton, 1, 0)
self.setLayout(self.layout)
addButton.clicked.connect(self.add)
def add(self):
self.button = {}
print(type(self.button))
for i in range(1, 8):
self.button = QtWidgets.QPushButton(str(i))
self.button.clicked.connect(lambda: self.show_all_row(self.sender().text()))
self.layout.addWidget(self.button)
@staticmethod
def show_all_row(txt):
print('here' + str(txt))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = DynAddObject()
form.show()
app.exec_()
你没给完整代码 我就参考https://blog.csdn.net/iblctw/article/details/106421326的demo,改了一下 看是不是你需要的效果 # -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import QtCore, QtWidgets, QtGui
'''
Pyqt 动态的添加控件
'''
class DynAddObject(QWidget):
def __init__(self, parent=None):
super(DynAddObject, self).__init__(parent)
addButton = QtWidgets.QPushButton(u"添加控件")
self.layout = QtWidgets.QGridLayout()
self.layout.addWidget(addButton, 1, 0)
self.setLayout(self.layout)
addButton.clicked.connect(self.add)
def add(self):
self.button = {}
print(type(self.button))
for i in range(1, 8):
self.button = QtWidgets.QPushButton(str(i))
self.button.clicked.connect(lambda: self.show_all_row(self.sender().text()))
self.layout.addWidget(self.button)
@staticmethod
def show_all_row(txt):
print('here' + str(txt))
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
form = DynAddObject()
form.show()
app.exec_()
我之前回答带链接在审核 ,给你一段参考吧
页:
[1]