我正在使用PyQt作为qgis插件。我希望有一些table / listview,其中第一列是open shapefile中的层,然后第二列是组合框,将从数据库输入中加载值。这是可能的,如果是这样,我需要使用listview,tableview或treeview吗?提前致谢!!
答案 0 :(得分:1)
如果我理解你,我前段时间做了类似的事情。
在我的情况下,我想用QDoubleSpinBoxes填充以下QTableWidget,并为每个QToubleWinBox设置特定的属性。
所以我在QDesigner中创建了一个“模型”空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
希望它有用。