我目前正在开发一个QGis插件(我使用插件生成器创建一个示例),这个插件应该在QTableWidget中显示所选VectorLayers的功能。我有两个文件cactus.py(主程序)和UI_cactus_dialog_base.py(在QtCreator上设计的Ui文件)。 问题是:从主程序我不能在QTableWidget上设置项目,我不知道为什么(我在QGis / Python编程方面很新,而且我对自我很困惑/ 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
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_cactus_dialog_base(QtGui.QDialog):
def setupUi(self, cactus_dialog_base):
cactus_dialog_base.setObjectName(_fromUtf8("cactus_dialog_base"))
cactus_dialog_base.resize(567, 504)
self.buffer = QtGui.QPushButton(cactus_dialog_base)
self.buffer.setGeometry(QtCore.QRect(290, 440, 262, 41))
self.buffer.setObjectName(_fromUtf8("buffer"))
self.layoutWidget = QtGui.QWidget(cactus_dialog_base)
self.layoutWidget.setGeometry(QtCore.QRect(10, 70, 541, 61))
self.layoutWidget.setObjectName(_fromUtf8("layoutWidget"))
self.gridLayout = QtGui.QGridLayout(self.layoutWidget)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.lbl_capa = QtGui.QLabel(self.layoutWidget)
self.lbl_capa.setObjectName(_fromUtf8("lbl_capa"))
self.gridLayout.addWidget(self.lbl_capa, 0, 0, 1, 1)
self.layer_name = QtGui.QLabel(self.layoutWidget)
self.layer_name.setObjectName(_fromUtf8("layer_name"))
self.gridLayout.addWidget(self.layer_name, 0, 1, 1, 1)
self.lbl_geometria = QtGui.QLabel(self.layoutWidget)
self.lbl_geometria.setObjectName(_fromUtf8("lbl_geometria"))
self.gridLayout.addWidget(self.lbl_geometria, 1, 0, 1, 1)
self.geometry_type = QtGui.QLabel(self.layoutWidget)
self.geometry_type.setObjectName(_fromUtf8("geometry_type"))
self.gridLayout.addWidget(self.geometry_type, 1, 1, 1, 1)
self.groupBox = QtGui.QGroupBox(cactus_dialog_base)
self.groupBox.setGeometry(QtCore.QRect(10, 150, 541, 201))
self.groupBox.setObjectName(_fromUtf8("groupBox"))
self.eje_tablewidget = QtGui.QTableWidget(self.groupBox)
self.eje_tablewidget.setGeometry(QtCore.QRect(10, 20, 521, 171))
self.eje_tablewidget.setObjectName(_fromUtf8("eje_tablewidget"))
self.eje_tablewidget.setColumnCount(18)
self.eje_tablewidget.setRowCount(0)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(0, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(1, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(2, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(3, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(4, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(5, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(6, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(7, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(8, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(9, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(10, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(11, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(12, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(13, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(14, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(15, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(16, item)
item = QtGui.QTableWidgetItem()
self.eje_tablewidget.setHorizontalHeaderItem(17, item)
self.retranslateUi(cactus_dialog_base)
QtCore.QObject.connect(self.buffer, QtCore.SIGNAL(_fromUtf8("clicked()")), cactus_dialog_base.show_record)
QtCore.QMetaObject.connectSlotsByName(cactus_dialog_base)
def retranslateUi(self, cactus_dialog_base):
cactus_dialog_base.setWindowTitle(_translate("cactus_dialog_base", "Cactus", None))
self.buffer.setText(_translate("cactus_dialog_base", "Asignar Buffer", None))
self.lbl_capa.setText(_translate("cactus_dialog_base", "Capa Actual:", None))
self.layer_name.setText(_translate("cactus_dialog_base", "Layer_Name", None))
self.lbl_geometria.setText(_translate("cactus_dialog_base", "Tipo Geometría:", None))
self.geometry_type.setText(_translate("cactus_dialog_base", "Geometry_Type", None))
self.groupBox.setTitle(_translate("cactus_dialog_base", "Ejes Seleccionados", None))
item = self.eje_tablewidget.horizontalHeaderItem(0)
item.setText(_translate("cactus_dialog_base", "ejee_id", None))
item = self.eje_tablewidget.horizontalHeaderItem(1)
item.setText(_translate("cactus_dialog_base", "ejee_tipo", None))
item = self.eje_tablewidget.horizontalHeaderItem(2)
item.setText(_translate("cactus_dialog_base", "ejee_nombre", None))
item = self.eje_tablewidget.horizontalHeaderItem(3)
item.setText(_translate("cactus_dialog_base", "ejee_nombre_completo", None))
item = self.eje_tablewidget.horizontalHeaderItem(4)
item.setText(_translate("cactus_dialog_base", "ejee_izq_inicio", None))
item = self.eje_tablewidget.horizontalHeaderItem(5)
item.setText(_translate("cactus_dialog_base", "ejee_izq_termino", None))
item = self.eje_tablewidget.horizontalHeaderItem(6)
item.setText(_translate("cactus_dialog_base", "ejee_der_inicio", None))
item = self.eje_tablewidget.horizontalHeaderItem(7)
item.setText(_translate("cactus_dialog_base", "ejee_der_termino", None))
item = self.eje_tablewidget.horizontalHeaderItem(8)
item.setText(_translate("cactus_dialog_base", "ejee_clase_funcional", None))
item = self.eje_tablewidget.horizontalHeaderItem(9)
item.setText(_translate("cactus_dialog_base", "ejee_categoria_velocidad", None))
item = self.eje_tablewidget.horizontalHeaderItem(10)
item.setText(_translate("cactus_dialog_base", "ejee_doble_sentido", None))
item = self.eje_tablewidget.horizontalHeaderItem(11)
item.setText(_translate("cactus_dialog_base", "ejee_transito_automovil", None))
item = self.eje_tablewidget.horizontalHeaderItem(12)
item.setText(_translate("cactus_dialog_base", "ejee_transito_bus", None))
item = self.eje_tablewidget.horizontalHeaderItem(13)
item.setText(_translate("cactus_dialog_base", "ejee_pavimentado", None))
item = self.eje_tablewidget.horizontalHeaderItem(14)
item.setText(_translate("cactus_dialog_base", "ejee_pago", None))
item = self.eje_tablewidget.horizontalHeaderItem(15)
item.setText(_translate("cactus_dialog_base", "ejee_link_id", None))
item = self.eje_tablewidget.horizontalHeaderItem(16)
item.setText(_translate("cactus_dialog_base", "ejee_geom", None))
item = self.eje_tablewidget.horizontalHeaderItem(17)
item.setText(_translate("cactus_dialog_base", "ejee_explica", None))
答案 0 :(得分:0)
有几种方法可以做你想要的。目前的代码中缺少一件事。
需要告诉QtableWidget何时使用setRowCount()
方法添加行。
所以实际上你需要在使用之前添加一行。列也是如此。
在你启动“for循环”之前,你需要setRowCount()
为零以“擦除表中的数据”,这就是QTableWidget的工作方式。
您还可以使用表格的rowCount和columnCount使其更具动态性,或使用eje.attribute["column_name"]
获取要添加到标准列位置的特定列。例如,你已经知道了
eje.attribue["user_name"]
是第3列,因此您不需要为列添加嵌套循环,而是可以 - >
self.tablewidget.setItem(QTableWidgetItem(self.tablewidget.rowCount()-1,3,eje.attribute["user_name"]))