[PyQt] Help: check content of combobox in horizontal header of QTableWidget - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] Help: check content of combobox in horizontal header of QTableWidget (/thread-28519.html) |
Help: check content of combobox in horizontal header of QTableWidget - mart79 - Jul-22-2020 Hi, I need to check the content of a combobox header in a QTableWidget. I found some snippets of code which I used to set up the table with the different comboboxes in the header of a QTableWidget but now I want to know what the selection (e.g. text/index) of the combobox is when I change it. How can I achieve this? The code so far is presented below: from PySide2 import QtCore, QtWidgets import numpy as np class HorizontalHeader(QtWidgets.QHeaderView): def __init__(self, values, parent=None): super(HorizontalHeader, self).__init__(QtCore.Qt.Horizontal, parent) self.setSectionsMovable(True) self.comboboxes = [] self.sectionResized.connect(self.handleSectionResized) self.sectionMoved.connect(self.handleSectionMoved) def showEvent(self, event): for i in range(2, self.count()): if i < len(self.comboboxes): combo = self.comboboxes[i] combo.clear() combo.addItems(["A", "B", 'L', 'T', 'V']) else: combo = QtWidgets.QComboBox(self) combo.addItems(["A", "B", 'L', 'T', 'V']) self.comboboxes.append(combo) combo.setGeometry(self.sectionViewportPosition(i), 0, self.sectionSize(i)-4, self.height()) combo.show() if len(self.comboboxes) > self.count(): for i in range(self.count(), len(self.comboboxes)): self.comboboxes[i].deleteLater() super(HorizontalHeader, self).showEvent(event) def handleSectionResized(self, i): for i in range(self.count()): j = self.visualIndex(i) logical = self.logicalIndex(j) self.comboboxes[i].setGeometry(self.sectionViewportPosition(logical), 0, self.sectionSize(logical)-4, self.height()) def handleSectionMoved(self, i, oldVisualIndex, newVisualIndex): for i in range(min(oldVisualIndex, newVisualIndex), self.count()): logical = self.logicalIndex(i) self.comboboxes[i].setGeometry(self.ectionViewportPosition(logical), 0, self.sectionSize(logical) - 5, height()) def fixComboPositions(self): for i in range(self.count()): self.comboboxes[i].setGeometry(self.sectionViewportPosition(i), 0, self.sectionSize(i) - 5, self.height()) class TableWidget(QtWidgets.QTableWidget): def __init__(self, columns, *args, **kwargs): super(TableWidget, self).__init__(*args, **kwargs) header = HorizontalHeader(self) self.setRowCount(100) self.setHorizontalHeader(header) # self.setRowCount(rows) self.setColumnCount(columns) self.setHorizontalHeaderLabels(['Loadcase', 'Step']) self.columns = columns self.createTable() def createTable(self): start = 0 for loadcase in range(10): steps = np.random.randint(1, 5) end = start + steps print(loadcase, steps, start, end) for row in range(start, end): item = QtWidgets.QTableWidgetItem(str(loadcase + 1)) item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) item.setCheckState(QtCore.Qt.Unchecked) self.setItem(row, 0, item) step = 0 for row in range(start, end): step += 1 item = QtWidgets.QTableWidgetItem(str(step)) self.setItem(row, 1, item) start = end self.data = np.random.rand(end, self.columns-2) for i, row_values in enumerate(self.data): for j, value in enumerate(row_values): self.setItem(i, j+2, QtWidgets.QTableWidgetItem(str(round(value,4)))) self.itemClicked.connect(self.handleItemClicked) self._list = [] def handleItemClicked(self, item): if item.checkState() == QtCore.Qt.Checked: if item.row() not in self._list: self._list.append(item.row()) print(self._list) else: if item.row() in self._list: self._list.remove(item.row()) print(self._list) def headerItemClicked(self, logicalIndex): column = self.currentItem().column() selectedName = self.horizontalHeaderItem(column) print(selectedName.text()) def scrollContentsBy(self, dx, dy): super(TableWidget, self).scrollContentsBy(dx, dy) if dx != 0: self.horizontalHeader().fixComboPositions() class App(QtWidgets.QWidget): def __init__(self): super(App,self).__init__() self.createTable() layout = QtWidgets.QVBoxLayout(self) layout.addWidget(self.table) self.showMaximized() def createTable(self): self.table = TableWidget(16) if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) ex = App() sys.exit(app.exec_()) RE: Help: check content of combobox in horizontal header of QTableWidget - deanhystad - Jul-26-2020 When you create the combo boxes you can connect the "currentIndexChanged" signal to some method in your HorizontalHeader class (def handleSectionIndexChanged?) If you need to expose that to your TableWidget, HorizonalHeader can provide a signal to notify the table when a header selection changes. |