简体   繁体   English

PyQgis:项目不是 QgsMapCanvas 上的预期类型

[英]PyQgis: item is not the expected type on a QgsMapCanvas

I am currently working on a PyQgis based standalone application and I need to add various QgsRubberBand to my Canvas.我目前正在开发基于 PyQgis 的独立应用程序,我需要将各种 QgsRubberBand 添加到我的 Canvas。

I made a subclass of it: LineAnnotation.我做了它的一个子类:LineAnnotation。

The problem is that when I use the method "QgsMapCanvas.itemAt(event.pos() )" on a "canvasPressEvent", it returns a "qgis._gui.QgsRubberBand" object, not a "LineAnnotation".问题是,当我在“canvasPressEvent”上使用方法“QgsMapCanvas.itemAt(event.pos())”时,它返回“qgis._gui.QgsRubberBand”object,而不是“LineAnnotation”。

Did I do something wrong?我做错什么了吗? The rest of my program can't work if it doesn't recognize that it's a LineAnnotation as it contains several new methods that I need to use.如果我的程序的 rest 不能识别它是一个 LineAnnotation,它就无法工作,因为它包含几个我需要使用的新方法。

Also I can't interact with the item at all, if I try to use one of the methods from QgsRubberBand, the application crashes.此外,如果我尝试使用 QgsRubberBand 中的其中一种方法,我根本无法与该项目进行交互,应用程序会崩溃。

Here is the code with the problem:这是有问题的代码:

from qgis.gui import QgsMapCanvas, QgsLayerTreeMapCanvasBridge, QgsRubberBand
from qgis.core import QgsApplication, QgsProject, QgsPointXY, QgsGeometry
from qgis.PyQt.QtGui import QColor
import sys

class LineAnnotation(QgsRubberBand):
    def __init__(self, canvas):
        QgsRubberBand.__init__(self, canvas)
        self.setColor(QColor("red") )
        self.setWidth(10)

class Interface(QgsMapCanvas):
    def __init__(self):
        QgsMapCanvas.__init__(self)
        self.setCanvasColor(QColor("#182F36") )
        project_path = "project_path"

        project = QgsProject.instance()
        project.read(project_path)
        layer_tree = QgsLayerTreeMapCanvasBridge(project.layerTreeRoot(), canvas=self)
        layer_tree.setAutoSetupOnFirstLayer(False)
        self.zoomToFeatureExtent(project.mapLayersByName('layer_name')[0].extent() )
        self.enableAntiAliasing(True)
        self.setAcceptDrops(True)
        self.setParallelRenderingEnabled(True)

        p1 = QgsPointXY(524670.46860305720474571, 5470375.41737424582242966)
        p2 = QgsPointXY(589864.10151600651443005, 5487531.63656186405569315)

        r = LineAnnotation(self)
        r.setToGeometry(QgsGeometry.fromPolylineXY([p1, p2]) )

    def mousePressEvent(self, event) -> None:
        item = self.itemAt(event.pos() )
        print(type(item) ) 
        # Output is "<class 'qgis._gui.QgsRubberBand'>"
        # Expected: "<class 'LineAnnotation'>"

class StackOverflow:
    def __init__(self):
        qgs = QgsApplication([], True)
        qgs.setDoubleClickInterval(250)
        qgs.initQgis()

        graphicUI = Interface()
        graphicUI.showMaximized()

        sys.exit(qgs.exec_() )

if __name__ == '__main__':
    app = StackOverflow()


> output: \<class 'qgis.\_gui.QgsRubberBand'\>
> Desired output: \<class 'lineAnnotation.LineAnnotation'\>

Problem seems to occur in versions prior to Qgis 3.26, my problem was solved after updating to latest version (3.28).问题似乎出现在 Qgis 3.26 之前的版本中,我的问题在更新到最新版本 (3.28) 后得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM