我在具有动态数据的 QtreeWidget 中的 QTreewidgetItem 中单击的编辑按钮
Edit button which i clicked in QTreewidgetItem in QtreeWidget which have dynamic data
我在QTreeWidget中有很多按钮,如何引用我点击的按钮或获取她的对象。
class MainWindow(QMainWindow, main_window_ui):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.treeWidget = QTreeWidget()
self.treeWidget.setColumnCount( len(HEADERS) )
self.treeWidget.setHeaderLabels( HEADERS )
self.treeWidget.setHeaderHidden(True)
for i in range(10):
item = QTreeWidgetItem()
self.treeWidget.addTopLevelItem(item)
item.setText(0, str(i))
item.setText(2, "123456789")
item.setText(3, "Ready")
item.setText(4, "2021-04-12")
checkbox = QCheckBox()
checkbox.setStyleSheet(checkboxst)
self.treeWidget.setItemWidget(item, 1, checkbox)
button = QPushButton()
button.setText("Start")
button.setStyleSheet(buttonst)
self.treeWidget.setItemWidget(item, 5, button)
# button.clicked.connect(self.getValue)
it = QTreeWidgetItem()
it.setText(0, "Edited")
item.addChild(it)
self.treeWidget.itemClicked.connect(self.getValue)
#snip...
def getValue(self):
index = self.treeWidget.currentItem()
我只需要更改我单击的按钮中的文本,我如何才能只引用我单击的按钮?
如果你想确定按下了哪个按钮,那么你可以使用 sender 方法:
button.clicked.connect(self.handle_clicked)
def handle_clicked(self):
button = self.sender()
if isinstance(button, QPushButton):
button.setText("Stop" if button.text() == "Start" else "Start")
我在QTreeWidget中有很多按钮,如何引用我点击的按钮或获取她的对象。
class MainWindow(QMainWindow, main_window_ui):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.treeWidget = QTreeWidget()
self.treeWidget.setColumnCount( len(HEADERS) )
self.treeWidget.setHeaderLabels( HEADERS )
self.treeWidget.setHeaderHidden(True)
for i in range(10):
item = QTreeWidgetItem()
self.treeWidget.addTopLevelItem(item)
item.setText(0, str(i))
item.setText(2, "123456789")
item.setText(3, "Ready")
item.setText(4, "2021-04-12")
checkbox = QCheckBox()
checkbox.setStyleSheet(checkboxst)
self.treeWidget.setItemWidget(item, 1, checkbox)
button = QPushButton()
button.setText("Start")
button.setStyleSheet(buttonst)
self.treeWidget.setItemWidget(item, 5, button)
# button.clicked.connect(self.getValue)
it = QTreeWidgetItem()
it.setText(0, "Edited")
item.addChild(it)
self.treeWidget.itemClicked.connect(self.getValue)
#snip...
def getValue(self):
index = self.treeWidget.currentItem()
我只需要更改我单击的按钮中的文本,我如何才能只引用我单击的按钮?
如果你想确定按下了哪个按钮,那么你可以使用 sender 方法:
button.clicked.connect(self.handle_clicked)
def handle_clicked(self):
button = self.sender()
if isinstance(button, QPushButton):
button.setText("Stop" if button.text() == "Start" else "Start")