PyQt6, QtreeWidget with QPushButton
-
Hi, you all.
I am new of PyQt6.
I have written a custom dialog box within a QTabWidget created with a loop for, and in every page there is a QTreeWidget with a pushbutton in every row.
I would like that at click command of the Qpushbutton, it could take the string inside the first column of the Qpushbutton clicked row.
This is my code:import pandas as pd import os from directory_configuration import vdmDataProcess from PyQt6.QtWidgets import (QWidget, QDialog, QVBoxLayout, QTabWidget, QTreeWidget, QTreeWidgetItem, QPushButton) from PyQt6.QtCore import Qt, QAbstractListModel class TrainingBasicProcessingPrint(QDialog): def __init__(self, basket): super().__init__() self.basket = basket self.setWindowTitle("Virtual Data Mart - " + self.basket) self.setFixedSize(1000, 600) self.layout = QVBoxLayout() # ----------- TAB CREATOR ----------- if os.path.exists(vdmDataProcess + "/" + self.basket): item_basket_list = os.listdir(vdmDataProcess + "/" + self.basket + "/training") self.notepad = QTabWidget() for item in item_basket_list: self.item_page = QWidget() self.notepad.addTab(self.item_page, item) self.item_layout = QVBoxLayout() self.chart_push_button = None self.tree = QTreeWidget() self.tree.setModel() self.tree.setAlternatingRowColors(True) self.tree.setColumnWidth(0, 180) self.tree.setColumnWidth(4, 180) self.tree.setColumnWidth(5, 180) self.tree.setHeaderLabels(["Titolo", "Profit/Loss Long", "Profit/Loss Short", "Profit/Loss", "N° Operazioni Long", "N° Operazioni Short", "Grafico"]) self.item_layout.addWidget(self.tree) self.item_page.setLayout(self.item_layout) self.tree_populate(item=item) self.layout.addWidget(self.notepad) self.setLayout(self.layout) def show_strategy_chart(self): page_name = self.notepad.setTabText() print(page_name) # for item_id in range(self.tree.topLevelItemCount()): # print(self.tree.focusWidget()) def tree_populate(self, item): list_information = [] self.tree.clear() list_item_content = os.listdir(vdmDataProcess + "/" + self.basket + "/training/" + item) for i, stock in enumerate(list_item_content): # READ STOCK DATA dataframe = pd.read_csv(vdmDataProcess + "/" + self.basket + "/training/" + item + "/" + stock, parse_dates=["Date"], index_col=0) profit_loss_long = round(dataframe["Euro variation Long"].sum(), 2) profit_loss_short = round(dataframe["Euro variation Short"].sum(), 2) profit_loss = round(profit_loss_long + profit_loss_short, 2) n_long_operation = dataframe["Euro variation Long"].count() n_short_operation = dataframe["Euro variation Short"].count() # END READ STOCK DATA stock = stock.rstrip("_.csv") list_information.append(stock) list_information.append(str(profit_loss_long)) list_information.append(str(profit_loss_short)) list_information.append(str(profit_loss)) list_information.append(str(n_long_operation)) list_information.append(str(n_short_operation)) item_information = QTreeWidgetItem(list_information) self.chart_push_button = QPushButton("grafico") self.chart_push_button.setFixedWidth(80) self.chart_push_button.setToolTip("Mostra grafico candlestick con strategia") self.chart_push_button.clicked.connect(self.show_strategy_chart) self.tree.insertTopLevelItems(i, [item_information]) self.tree.sortByColumn(0, Qt.SortOrder.AscendingOrder) self.tree.setItemWidget(item_information, 6, self.chart_push_button) list_information.clear() def start_basic_training_print(basket): print_dialog = TrainingBasicProcessingPrint(basket=basket) print_dialog.exec()
-
Hi,
The brute force version would be to search through the column which button matches the one that was clicked.
A slightly better version would be to store the row as a dynamic property of the button.The really good way is to implement a custom QStyledItemDelegate. That would reduce the number of widgets to manage and also follow the recommendations about the use of cell widgets. Bonus point, you would have the informations you need already accessible rather than having to do some convoluted associations or searches.