• <legend id='dLTay'><style id='dLTay'><dir id='dLTay'><q id='dLTay'></q></dir></style></legend>

  • <small id='dLTay'></small><noframes id='dLTay'>

      <bdo id='dLTay'></bdo><ul id='dLTay'></ul>
  • <tfoot id='dLTay'></tfoot>
      1. <i id='dLTay'><tr id='dLTay'><dt id='dLTay'><q id='dLTay'><span id='dLTay'><b id='dLTay'><form id='dLTay'><ins id='dLTay'></ins><ul id='dLTay'></ul><sub id='dLTay'></sub></form><legend id='dLTay'></legend><bdo id='dLTay'><pre id='dLTay'><center id='dLTay'></center></pre></bdo></b><th id='dLTay'></th></span></q></dt></tr></i><div id='dLTay'><tfoot id='dLTay'></tfoot><dl id='dLTay'><fieldset id='dLTay'></fieldset></dl></div>
      2. QTableWidget - 自动公式驱动的单元格

        时间:2023-08-05

        1. <tfoot id='Y7jC8'></tfoot>

        2. <legend id='Y7jC8'><style id='Y7jC8'><dir id='Y7jC8'><q id='Y7jC8'></q></dir></style></legend>
            <tbody id='Y7jC8'></tbody>
            <bdo id='Y7jC8'></bdo><ul id='Y7jC8'></ul>

            <small id='Y7jC8'></small><noframes id='Y7jC8'>

            <i id='Y7jC8'><tr id='Y7jC8'><dt id='Y7jC8'><q id='Y7jC8'><span id='Y7jC8'><b id='Y7jC8'><form id='Y7jC8'><ins id='Y7jC8'></ins><ul id='Y7jC8'></ul><sub id='Y7jC8'></sub></form><legend id='Y7jC8'></legend><bdo id='Y7jC8'><pre id='Y7jC8'><center id='Y7jC8'></center></pre></bdo></b><th id='Y7jC8'></th></span></q></dt></tr></i><div id='Y7jC8'><tfoot id='Y7jC8'></tfoot><dl id='Y7jC8'><fieldset id='Y7jC8'></fieldset></dl></div>

                  本文介绍了QTableWidget - 自动公式驱动的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以将一个单元格设为公式驱动单元格并自动更新?类似于 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模板网!

                  上一篇:在 PyQt5 中创建复杂的自定义小部件并将其添加到 下一篇:PyQt5 从 PyQt4 转换信号代码

                  相关文章

                  最新文章

                    <bdo id='lVnDb'></bdo><ul id='lVnDb'></ul>

                  1. <tfoot id='lVnDb'></tfoot>

                      <small id='lVnDb'></small><noframes id='lVnDb'>

                      <legend id='lVnDb'><style id='lVnDb'><dir id='lVnDb'><q id='lVnDb'></q></dir></style></legend>

                      <i id='lVnDb'><tr id='lVnDb'><dt id='lVnDb'><q id='lVnDb'><span id='lVnDb'><b id='lVnDb'><form id='lVnDb'><ins id='lVnDb'></ins><ul id='lVnDb'></ul><sub id='lVnDb'></sub></form><legend id='lVnDb'></legend><bdo id='lVnDb'><pre id='lVnDb'><center id='lVnDb'></center></pre></bdo></b><th id='lVnDb'></th></span></q></dt></tr></i><div id='lVnDb'><tfoot id='lVnDb'></tfoot><dl id='lVnDb'><fieldset id='lVnDb'></fieldset></dl></div>