是否有一个PyQt表/列表视图可以在一列中用组合框动态填充?
问题描述:
我正在使用PyQt的qgis插件。我想有一些表/列表视图,第一列是来自打开的shapefile的图层,然后第二列是将从数据库输入中加载值的组合框。这是可能的,如果是这样,我需要使用listview,tableview或treeview?提前致谢!!是否有一个PyQt表/列表视图可以在一列中用组合框动态填充?
答
如果我正确理解你,我已经做了一些非常相似的事情。
在我的情况下,我想用QDoubleSpinBoxes填充以下QTableWidget并为它们中的每一个设置特定的属性。
所以我在QDesigner做了一个 “模式” 空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
循环,将setValue添加到QDoubleSpinBox。此外,您还可以使用setVerticalHeaderLabels
和setHorizontalHeaderLabels
方法通过代码设置标题标签。
即使我使用pyqt,我通常会使用pyside的文档,因为它们大部分时间都是平等的,而pyside的读取和浏览更容易。 QTableWidget Docs
希望它有帮助。
非常有帮助!谢谢! – InsertDisplayName
@InsertDisplayName ...那么你可以接受答案,不是吗? –