問題描述
是否可以將一個單元格設為公式驅動單元格并自動更新?類似于 Excel.
Is it possible to make one cell a formula driven cell and have it update automatically? Similar to Excel.
例如,我希望用戶填寫兩個單元格,然后當用戶填寫兩個單元格時,第三個單元格將自動劃分.我希望它不連接到按鈕.
For example, I want user to fill out two cells, and then a third cell will automatically divide when user fills both cells. I'd like it to be NOT connected to a button.
QTable 截圖
TableWidget 的代碼:
Code for TableWidget:
self.tableWidget = {}
for i in range(int(self.numberLine.text())):
self.tableWidget[i] = QTableWidget()
self.tableWidget[i].setRowCount(5)
self.tableWidget[i].setColumnCount(3)
self.tableWidget[i].setHorizontalHeaderLabels(['OEM (Case {})'.format(i+1), 'ZVI (Case {})'.format (i+1), 'Improvement % '])
self.tableWidget[i].setVerticalHeaderLabels(['Flow (MMSCFD)', 'HP', 'Specific Power (HP/MMSCFD)', 'Discharge Temp (F)', ''])
self.tableWidget[i].setFixedSize(QtCore.QSize(480, 180))
self.gridLayout_14.addWidget(self.tableWidget[i])
推薦答案
一個優雅的解決方案是創建一個繼承自 QTableWidget
的自定義類,在其中連接 itemChanged
信號,每次單元格更改值時都會發出此信號(返回更改的項目,但僅用于驗證默認列是否已更改).
An elegant solution is to create a custom class that inherits from QTableWidget
, where you connect the itemChanged
signal, this is issued each time the cell changes value (this returns the changed item but will use it only to verify that the default columns are the ones have been changed).
除了不存在用戶將不同值放入浮動對象的問題之外,我們還將使用 QDoubleValidator,為此我們創建了一個自定義 QItemDelegate.
In addition to not having problems that the user places different values to a floating we will use a QDoubleValidator, for that we create a custom QItemDelegate.
class FloatDelegate(QItemDelegate):
def __init__(self, _from, _to, _n_decimals, parent=None):
QItemDelegate.__init__(self, parent=parent)
self._from = _from
self._to = _to
self._n_decimals = _n_decimals
def createEditor(self, parent, option, index):
lineEdit = QLineEdit(parent)
_n_decimals = 2
validator = QDoubleValidator(self._from, self._to, self._n_decimals, lineEdit)
lineEdit.setValidator(validator)
return lineEdit
class CustomTableWidget(QTableWidget):
_from = 0
_to = 10**5
_n_decimals = 2
def __init__(self, i, parent=None):
QTableWidget.__init__(self, 5, 3, parent=parent)
self.setItemDelegate(FloatDelegate(self._from, self._to, self._n_decimals, self))
self.setHorizontalHeaderLabels(['OEM (Case {})'.format(i+1), 'ZVI (Case {})'.format (i+1), 'Improvement % '])
self.setVerticalHeaderLabels(['Flow (MMSCFD)', 'HP', 'Specific Power (HP/MMSCFD)', 'Discharge Temp (F)', ''])
self.setFixedSize(QSize(480, 180))
self.itemChanged.connect(self.onItemChanged)
def onItemChanged(self, item):
# items (2, 0) = (1, 0) / (0, 0)
if item.column() == 0 and (item.row() == 0 or item.row()==1):
num = self.item(1, 0)
den = self.item(0, 0)
if num and den:
resp = float(num.data(Qt.DisplayRole))/float(den.data(Qt.DisplayRole))
rest_string = str(round(resp, self._n_decimals))
it = QTableWidgetItem(rest_string, QTableWidgetItem.Type)
self.setItem(2, 0, it)
例子:
class Widget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent=parent)
self.setLayout(QGridLayout())
for i in range(2):
self.layout().addWidget(CustomTableWidget(i))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Widget()
window.show()
sys.exit(app.exec_())
在你的情況下:
self.tableWidget = {}
for i in range(int(self.numberLine.text())):
self.tableWidget[i] = CustomTableWidget(i)
self.gridLayout_14.addWidget(self.tableWidget[i])
我們可以將 QLineEdit 更改為 QDoubleSpinBox,而不是使用驗證器.
Another option instead of using validators, we can change the QLineEdit to QDoubleSpinBox.
def createEditor(self, parent, option, index):
w = QDoubleSpinBox(parent)
_n_decimals = 2
w.setMinimum(self._from)
w.setMaximum(self._to)
w.setDecimals(self._n_decimals)
return w
這篇關于QTableWidget - 自動公式驅動的單元格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!