I created a simple tree and a button. What I want to do is increase the cat's power by 1 each time I press the button. Assuming that I already know cat's position (root -> second item -> second item), how can I update the progress bar in increaseValue
?
import sys
from PySide6.QtCore import QSize, Qt, Slot
from PySide6.QtWidgets import QApplication, QMainWindow, QTreeWidget, QTreeWidgetItem, QProgressBar, QPushButton, \
QVBoxLayout, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("My App")
data = {"Canidae": {"Wolf": 70, "Fox": 40, "Dog": 30},
"Felidae": {"Tiger": 100, "Cat": 20}};
app = QApplication(sys.argv)
tree = QTreeWidget()
tree.setColumnCount(2)
tree.setHeaderLabels(["Animal", "Power"])
tree.setColumnWidth(0, 150)
tree.setColumnWidth(1, 200)
items = []
for key, values in data.items():
item = QTreeWidgetItem([key])
for key2, values2 in values.items():
pb = QProgressBar();
pb.setRange(0, 100);
pb.setValue(values2);
pb.setAutoFillBackground(True);
child = QTreeWidgetItem([key2])
item.addChild(child)
tree.setItemWidget(child, 1, pb);
items.append(item)
@Slot()
def increaseValue():
print("Button clicked");
tree.insertTopLevelItems(0, items)
tree.expandAll();
testButton = QPushButton("Test");
testButton.clicked.connect(increaseValue)
layout = QVBoxLayout();
layout.addWidget(tree);
layout.addWidget(testButton);
window = MainWindow()
window.resize(400, 300)
window.show()
dummy = QWidget()
dummy.setLayout(layout)
window.setCentralWidget(dummy)
app.exec()