Im目前正在开发一个QGis插件(我使用pluginbuilder创建了一个示例),这个插件应该显示QTableWidget中选定向量层的特性。我有两份文件仙人掌.py(主程序)和“仙人掌”对话框_基准.py(QtCreator上设计的Ui文件)。 问题是:在主程序中,我不能在QTableWidget上设置项目,而且我不知道为什么(,我对QGis/Python编程非常陌生,我对self/iface/ui实例非常困惑。救命啊!在
cactus.py
class Cactus:
def __init__(self, iface):
self.iface = iface
self.tablewidget = QTableWidget()
# initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
# initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'Cactus_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
if qVersion() > '4.3.3':
QCoreApplication.installTranslator(self.translator)
# Create the dialog (after translation) and keep reference
self.dlg = Ui_cactus_dialog_base()
self.dlg.show_record = self.show_record
self.dlg.eje_tablewidget = self.tablewidget
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&Cactus')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'Cactus')
self.toolbar.setObjectName(u'Cactus')
# noinspection PyMethodMayBeStatic
def tr(self, message):
return QCoreApplication.translate('Cactus', message)
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/Cactus/icon.png'
self.add_action(
icon_path,
text=self.tr(u'Cactus - Creacion manzanas a partir de ejes'),
callback=self.run,
parent=self.iface.mainWindow())
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&Cactus'),
action)
self.iface.removeToolBarIcon(action)
# remove the toolbar
del self.toolbar
def run(self):
self.dlg.setupUi(self.dlg)
"""Run method that performs all the real work"""
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
pass
@pyqtSlot()
def show_record(self):
layer = self.iface.activeLayer()
ejes = layer.selectedFeatures()
print len(ejes)
i = 0
j = 0
for eje in ejes:
i = i + 1
for column in eje:
item = QTableWidgetItem('ejee_id')
self.tablewidget.setItem(i, j, item)
print item
j = j + 1
UI_cactus_dialog_base.py
有几种方法可以做你想做的事。在目前的代码中,有一件事是缺失的。在
需要告诉QtableWidget何时使用
setRowCount()
方法添加行。在你需要先加一行。柱子也是一样。在
在开始“for循环”之前,需要将
setRowCount()
设置为零,以便“从表中删除数据”,因为QTableWidget就是这样工作的。在您还可以通过使用表的rowCount和columnCount使其更具动态性,或者使用
eje.attribute["column_name"]
在标准列位置获取要添加的特定列。例如,你已经知道是第三列,因此不需要对列执行嵌套循环,而是可以->
^{pr2}$相关问题 更多 >
编程相关推荐