Ryanc88
Ryanc88

Reputation: 192

PyQt6 override QTableWidget "select all" functionality with custom widget

I'm trying to build a custom watchlist widget using QTableWidget. By default, in the top left corner, if you press the empty box, it will select all items in the table. I want to replace this empty box with my own custom widget to add a QComboBox dropdown. Is this even possible? If so, how can I build this?

enter image description here

Example of what I'm trying to build. Clicking the corner box should open a dropdown that has various things

enter image description here

I already tried to override the selectAll() functionality to do other actions but I'm unable to replace the entire section with a new custom widget

Code

from PyQt6.QtWidgets import QTableWidget, QApplication, \
    QTableWidgetItem, QMainWindow, QWidget, QHBoxLayout

class CustomWatchlist(QTableWidget):
    def __init__(self):
        super(CustomWatchlist, self).__init__()

        self.horizontalHeader().setDefaultSectionSize(100)
        self.setColumnCount(2)
        
        self.header_labels = ['Symbol', 'Mark']
        self.setHorizontalHeaderLabels(self.header_labels)
        
        self.data = {'MSFT': '100.0',
                     'AAPL': '205.5',
                     'QQQ': '350.0'}
        
        for index, symbol in enumerate(self.data):
            row_position = self.rowCount()
            self.insertRow(row_position)
            self.setItem(index, 0, QTableWidgetItem(symbol))
            self.setItem(index, 1, QTableWidgetItem(self.data[symbol]))
    
if __name__ == '__main__':
    app = QApplication([])
    
    main_window = QMainWindow()
    main_window.setMinimumWidth(200)
    main_window.setMinimumHeight(200)
    
    central_widget = QWidget()
    main_layout = QHBoxLayout()
    central_widget.setLayout(main_layout)
    main_window.setCentralWidget(central_widget)
    
    table = CustomWatchlist()
    
    main_layout.addWidget(table)
   
    main_window.show()
    app.exec()

Upvotes: 1

Views: 24

Answers (0)

Related Questions