是否有 PyQt table/ listview 可以在其中一列中动态填充组合框?
Is there a PyQt table/ listview that can be filled dynamically with comboboxes in one of the columns?
我正在使用 PyQt 作为 qgis 插件。我想要某种 table/ 列表视图,其中第一列是来自打开的 shapefile 的层,然后第二列是组合框,它们将加载来自数据库输入的值。这可能吗?如果可以,我是否需要使用 listview、tableview 或 treeview?提前致谢!!
如果我理解正确的话,我刚才也做过类似的事情。
在我的例子中,我想用 QDoubleSpinBoxes 填充以下 QTableWidget 并为它们中的每一个设置特定的属性。
所以我在 QDesigner 中创建了一个 "model" 空 QTableWidget,在我的代码中我使用了以下函数:
填充 QTableWidget
def QTableWidget_populate(self, table):
# Create an empty matrix with the same dimensions as your table
self.ele_mat = [
[0 for x in range(table.columnCount())] for x in range(table.rowCount())]
# For each cell in the table define its characteristics
for row in xrange(table.rowCount()):
for column in xrange(table.columnCount()):
# Define row name prefix
if row == 0:
element = 'Begin_'
elif row == 1:
element = 'MidMinus_'
elif row == 2:
element = 'MidPlus_'
elif row == 3:
element = 'End_'
# Define columns paraters
if column == 0:
element += 'Pow' # Name column sufix
param1 = '2' # setDecimals settings
param2 = '-99,99' # setRange
param3 = '"dBm"' # seSuffix
elif column == 1:
element += 'PD'
param1 = '0'
param2 = '0,999'
param3 = '"uA"'
elif column == 2:
element += 'TMVoltage'
param1 = '2'
param2 = '-99,99'
param3 = '"V"'
elif column == 3:
element += 'Wav'
param1 = '1'
param2 = '0,2000'
param3 = '"nm"'
# Popule matrix with dict of parameters
self.ele_mat[row][column] = {
'name': element, 'type': 'QtGui.QDoubleSpinBox()',
'Adjustments': ['setDecimals', 'setRange', 'setSuffix'],
'Params': [param1, param2, param3]}
# Using the matrix populate the QTableWidget
for row in xrange(table.rowCount()):
for column in xrange(table.columnCount()):
# Create Widget with name and type defined in the matrix
exec('self.{} = {}' .format(
self.ele_mat[row][column]['name'],
self.ele_mat[row][column]['type']))
# Give it its settings
for adjustment, param in zip(self.ele_mat[row][column]['Adjustments'],
self.ele_mat[row][column]['Params']):
exec('self.{}.{}({})' .format(self.ele_mat[
row][column]['name'], adjustment, param))
# Set the created widget to the QTableWidget
table.setCellWidget(row, column, eval(
'self.{}' .format(self.ele_mat[row][column]['name'])))
# For better clarity in GUI
table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
table.verticalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
我不知道您正在阅读的文件的格式如何,但我确定您可以根据文件中的条目在我的函数中添加一个 for
循环以将值设置为 QDoubleSpinBox .此外,您还可以使用 setVerticalHeaderLabels
和 setHorizontalHeaderLabels
方法通过代码设置 Header 标签。
尽管我使用 pyqt,但我通常使用 pyside 的文档,因为它们在大多数情况下是相同的,而且 pyside 的文档更容易阅读和浏览。
QTableWidget Docs
希望对您有所帮助。
我正在使用 PyQt 作为 qgis 插件。我想要某种 table/ 列表视图,其中第一列是来自打开的 shapefile 的层,然后第二列是组合框,它们将加载来自数据库输入的值。这可能吗?如果可以,我是否需要使用 listview、tableview 或 treeview?提前致谢!!
如果我理解正确的话,我刚才也做过类似的事情。
在我的例子中,我想用 QDoubleSpinBoxes 填充以下 QTableWidget 并为它们中的每一个设置特定的属性。
所以我在 QDesigner 中创建了一个 "model" 空 QTableWidget,在我的代码中我使用了以下函数:
填充 QTableWidget
def QTableWidget_populate(self, table):
# Create an empty matrix with the same dimensions as your table
self.ele_mat = [
[0 for x in range(table.columnCount())] for x in range(table.rowCount())]
# For each cell in the table define its characteristics
for row in xrange(table.rowCount()):
for column in xrange(table.columnCount()):
# Define row name prefix
if row == 0:
element = 'Begin_'
elif row == 1:
element = 'MidMinus_'
elif row == 2:
element = 'MidPlus_'
elif row == 3:
element = 'End_'
# Define columns paraters
if column == 0:
element += 'Pow' # Name column sufix
param1 = '2' # setDecimals settings
param2 = '-99,99' # setRange
param3 = '"dBm"' # seSuffix
elif column == 1:
element += 'PD'
param1 = '0'
param2 = '0,999'
param3 = '"uA"'
elif column == 2:
element += 'TMVoltage'
param1 = '2'
param2 = '-99,99'
param3 = '"V"'
elif column == 3:
element += 'Wav'
param1 = '1'
param2 = '0,2000'
param3 = '"nm"'
# Popule matrix with dict of parameters
self.ele_mat[row][column] = {
'name': element, 'type': 'QtGui.QDoubleSpinBox()',
'Adjustments': ['setDecimals', 'setRange', 'setSuffix'],
'Params': [param1, param2, param3]}
# Using the matrix populate the QTableWidget
for row in xrange(table.rowCount()):
for column in xrange(table.columnCount()):
# Create Widget with name and type defined in the matrix
exec('self.{} = {}' .format(
self.ele_mat[row][column]['name'],
self.ele_mat[row][column]['type']))
# Give it its settings
for adjustment, param in zip(self.ele_mat[row][column]['Adjustments'],
self.ele_mat[row][column]['Params']):
exec('self.{}.{}({})' .format(self.ele_mat[
row][column]['name'], adjustment, param))
# Set the created widget to the QTableWidget
table.setCellWidget(row, column, eval(
'self.{}' .format(self.ele_mat[row][column]['name'])))
# For better clarity in GUI
table.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
table.verticalHeader().setResizeMode(QtGui.QHeaderView.Stretch)
我不知道您正在阅读的文件的格式如何,但我确定您可以根据文件中的条目在我的函数中添加一个 for
循环以将值设置为 QDoubleSpinBox .此外,您还可以使用 setVerticalHeaderLabels
和 setHorizontalHeaderLabels
方法通过代码设置 Header 标签。
尽管我使用 pyqt,但我通常使用 pyside 的文档,因为它们在大多数情况下是相同的,而且 pyside 的文档更容易阅读和浏览。 QTableWidget Docs
希望对您有所帮助。