Question:
How to implement the function of displaying the text that is stored in a certain cell in QTreeWidget
when clicking on this cell. Those. the user clicks LMB on any cell and the text from this cell is displayed on the screen (or in my case so far in the console). So far, I figured out how to display text when clicking on a line in a field and display a specific column
import sys
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QApplication, QWidget
def asd(a):
print(a.text(1))
if __name__ == '__main__':
app = 0
if QApplication.instance():
app = QApplication.instance()
else:
app = QApplication(sys.argv)
l1 = QTreeWidgetItem(["String A", "String B", "String C"])
l2 = QTreeWidgetItem(["String AA", "String BB", "String CC"])
for i in range(3):
l1_child = QTreeWidgetItem(["Child A" + str(i), "Child B" + str(i), "Child C" + str(i)])
l1.addChild(l1_child)
for j in range(2):
l2_child = QTreeWidgetItem(["Child AA" + str(j), "Child BB" + str(j), "Child CC" + str(j)])
l2.addChild(l2_child)
w = QWidget()
w.resize(510, 210)
tw = QTreeWidget(w)
tw.resize(500, 200)
tw.setColumnCount(3)
tw.setHeaderLabels(["Column 1", "Column 2", "Column 3"])
tw.addTopLevelItem(l1)
tw.addTopLevelItem(l2)
tw.itemClicked.connect(asd)
w.show()
sys.exit(app.exec_())
Answer:
def asd(a, c):
print(a.text(c))
UPDATE:
def asd(a, c):
v = []
for i in range(a.columnCount()):
v.append(a.text(i))
print(*v)