English 中文(简体)
扩展 QGIS 插件构建器结果, 组合框在引用新对象时失败
原标题:Extending QGIS Plugin builder result with a Combobox fails in referencing the new object

I have extended the UI file resulting from the Plugin builder with Qt Creator. Just added some checkboxes and a combobox, named layercombo to the form. The application is named jacktest.py. It uses an intermediate file jackdialog.py (generated from the plugin builder, left unchanged).

Compiled the UI file and the resource file. Then added some code to the plugin and tested this. It s no problem to get the available layer names in a QMessagebox. But how to add these to the combobox ? Should be simple, but no option succeeds in referencing the combobox.

错误消息: 属性错误: jacktest 实例没有属性层孔布 。

我最近尝试的结果是:

# run method that performs all the real work
def run(self):

    # create and show the dialog
    dlg = jacktestDialog()
    # show the dialog
    dlg.show()
    result = dlg.exec_()
    for layer in self.iface.legendInterface().layers():
        if layer.type() == QgsMapLayer.VectorLayer:
           QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
           self.layercombo.Items.Insert(0, layer.name())
    # See if OK was pressed
    if result == 1:
        # do something useful (delete the line containing pass and
        # substitute with your code
        pass
问题回答

当您正在设置 discombo 项时, 您正试图引用当前类( 这不是您的对话框) 。

替换:

self.layercombo.Items.Insert(0, layer.name())

dlg.ui.layercombo.Items.Insert(0, layer.name())

您的代码仍然无法正确工作, 因为 exec_ () 正在屏蔽, 等待返回, 以便您在隐性对话框中添加项目 。

试试这个代替:

# create and show the dialog
dlg = jacktestDialog()
# show the dialog
for layer in self.iface.legendInterface().layers():
    if layer.type() == QgsMapLayer.VectorLayer:
       QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
       dlg.ui.layercombo.Items.Insert(0, layer.name())
result = dlg.exec_()

在运行模块中开发一个信号( 代码: def run (自) :)

QObject. connect( dlg. ui. layercombo, Signal( 目前的 Index changed (int) ), self. select_ one)

选择一的代码是:

def select_one(self):
    comboindex = dlg.ui.layercombo.currentIndex()
    QMessageBox.information(self.iface.mainWindow(), "Info", comboindex)

错误消息 :

comboindex = dlg.ui.layercombo.currentIndex() NameError: global name dlg is not defined

假设我必须在函数调用中引用 dlg 作为参数, 但直到现在才起作用 。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签