Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. PyQt6, QtreeWidget with QPushButton

PyQt6, QtreeWidget with QPushButton

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 559 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Luigi_V
    wrote on last edited by
    #1

    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()
    
    

    Screenshot 2023-10-25 alle 17.51.26.png

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        L Offline
        L Offline
        Luigi_V
        wrote on last edited by
        #3

        @SGaist
        Hi,
        I do not know how to use QStyledItemDelegate.
        Can You suggest me a book where I learn how to use it?
        thanks.

        SGaistS 1 Reply Last reply
        0
        • L Luigi_V

          @SGaist
          Hi,
          I do not know how to use QStyledItemDelegate.
          Can You suggest me a book where I learn how to use it?
          thanks.

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Luigi_V you should start by the Qt Documentation. There are examples for that class.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved