From cba720730dae98201a1e2e61924b2f2e5bd6fb7f Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Tue, 5 Mar 2013 14:02:55 +1100 Subject: [PATCH] Some extra bits - add sizeHint, make not editable, make columns sortable --- pyqtgraph/widgets/TableWidget.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pyqtgraph/widgets/TableWidget.py b/pyqtgraph/widgets/TableWidget.py index 4c6a77ce..3fa02d59 100644 --- a/pyqtgraph/widgets/TableWidget.py +++ b/pyqtgraph/widgets/TableWidget.py @@ -26,6 +26,7 @@ class TableWidget(QtGui.QTableWidget): QtGui.QTableWidget.__init__(self, *args) self.setVerticalScrollMode(self.ScrollPerPixel) self.setSelectionMode(QtGui.QAbstractItemView.ContiguousSelection) + self.setSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred) self.clear() self.contextMenu = QtGui.QMenu() self.contextMenu.addAction('Copy Selection').triggered.connect(self.copySel) @@ -44,6 +45,8 @@ class TableWidget(QtGui.QTableWidget): def setData(self, data): self.clear() self.appendData(data) + self.setSortingEnabled(True) + self.resizeColumnsToContents() def appendData(self, data): """Types allowed: @@ -135,9 +138,23 @@ class TableWidget(QtGui.QTableWidget): s = str(val) item = QtGui.QTableWidgetItem(s) item.value = val + # by default this is enabled, selectable & editable, but + # we don't want editable + item.setFlags(QtCore.Qt.ItemIsSelectable|QtCore.Qt.ItemIsEnabled) self.items.append(item) self.setItem(row, col, item) - + + def sizeHint(self): + # based on http://stackoverflow.com/a/7195443/54056 + width = sum(self.columnWidth(i) for i in range(self.columnCount())) + width += self.verticalHeader().sizeHint().width() + width += self.verticalScrollBar().sizeHint().width() + width += self.frameWidth() * 2 + height = sum(self.rowHeight(i) for i in range(self.rowCount())) + height += self.verticalHeader().sizeHint().height() + height += self.horizontalScrollBar().sizeHint().height() + return QtCore.QSize(width, height) + def serialize(self, useSelection=False): """Convert entire table (or just selected area) into tab-separated text values""" if useSelection: