我正在使用PyQt 4编写一个单位转换器。这里,我遇到了一个问题。
class UnitConverter(QtGui.QComboBox):
def __init__(self, parent=None):
super(UnitConverter, self).__init__(parent)
self.unitsdict = {
"mass": {"g": 1, "kg": 1000, "t": 1000000, "oz": 28.3495231, "lb": 453.59237},
"pressure": {"pa": 1, "kpa": 1000, "mpa": 1000000, "bar": 100000, "atm": 101325,
"tor": 133.3223684, "psi": 6894.757, "at": 98066.5}
}
def combo(self, measurement):
for key in self.unitsdict[measurement]:
self.addItem(self.tr(key), self.unitsdict[measurement][key])
def comboConnect(self, table, row, column):
self.names = locals()
#tablevar = "self.table_" + str(row) + "_" + str(column)
self.rowvar = "self.row_" + str(row) + "_" + str(column)
self.columnvar = "self.column_" + str(row) + "_" + str(column)
self.names[self.rowvar] = row
self.names[self.columnvar] = column
self.table = table
self.base = self.itemData(self.currentIndex())
self.currentIndexChanged.connect(self.change)
def change(self):
tobase = self.itemData(self.currentIndex())
try:
self.value = float(self.table.item(self.names[self.rowvar], self.names[self.columnvar]).text())
tovalue = '%f' % (self.value * self.base / tobase)
changeitem = QtGui.QTableWidgetItem(str(tovalue))
self.table.setItem(self.names[self.rowvar], self.names[self.columnvar], changeitem)
except:
pass
self.base = tobase
当只有一个QTableWidgetItem连接到它时,它可以工作。
table = QTableWidget(10, 3)
combo = Unit converter()
combo.combo("pressure")
table.setCellWidget(5, 0, combo)
combo.comboConnect(table, 5, 1)
但是当我连接另一个QTableWidgetItem时,第一个意外地不再起作用。
table = QTableWidget(10, 3)
combo = Unit converter()
combo.combo("pressure")
table.setCellWidget(5, 0, combo)
combo.comboConnect(table, 5, 1)
combo.comboConnect(table, 5, 2)
所以,我想知道问题是什么。或者,我该如何使表(5,1)再次工作?
答案 0 :(得分:1)
主要问题是你要覆盖很多变量,所以你只看到最后保存的数据,例如self.rowvar和self.columnvar保存最后一行和列,而不是更新你必须将它添加到列表或其他东西类似。
class UnitConverter(QtGui.QComboBox):
def __init__(self, table, parent=None):
super(UnitConverter, self).__init__(parent)
self.unitsdict = {
"mass": {"g": 1, "kg": 1000, "t": 1000000, "oz": 28.3495231, "lb": 453.59237},
"pressure": {"pa": 1, "kpa": 1000, "mpa": 1000000, "bar": 100000, "atm": 101325,
"tor": 133.3223684, "psi": 6894.757, "at": 98066.5}
}
self.table = table
self.base = -1
self.items = []
self.currentIndexChanged.connect(self.change)
def combo(self, measurement):
for key in self.unitsdict[measurement]:
self.addItem(self.tr(key), self.unitsdict[measurement][key])
self.base = self.itemData(self.currentIndex())
def comboConnect(self, row, column):
self.items.append((row, column))
def change(self, ix,):
tobase = self.itemData(ix)
factor = self.base / tobase
self.base = tobase
for r, c in self.items:
it = self.table.item(r, c)
try:
value = float(it.text())
tovalue = '%f' % (value * factor)
it.setText(tovalue)
except:
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
table = QtGui.QTableWidget(10, 3)
combo = UnitConverter(table)
combo.combo("pressure")
table.setCellWidget(5, 0, combo)
combo.comboConnect(5, 1)
combo.comboConnect(5, 2)
table.show()
sys.exit(app.exec_())