1

I added a subwindow in a MDI area containing a QgsMapCanvas. Defined the clic tool QgsMapToolEmitPoint.

But when I click on the canvas there's no response, al least it should print 'clicked'. ¿¿May be something is missing?? (debug with pycharm on windows)

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from qgis.core import *
from qgis.gui import *
from qgis.utils import *

from resources.main2 import *

class myMainClass(QtWidgets.QMainWindow, ):
    def __init__(self, parent=None):
        super(myMainClass, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.swMap = QgsMapCanvas()
        self.swMap.setCanvasColor(Qt.white)
        self.swMap.enableAntiAliasing(True)
        self.swMap.setSelectionColor(QColor(255, 255, 0, 50))
        self.ui.mdiArea.addSubWindow(self.swMap)

        pointTool = QgsMapToolEmitPoint(self.swMap)
        pointTool.canvasClicked.connect(self.changeColor)
        self.swMap.setMapTool(pointTool)

    def changeColor(self,point, mouse_button):
        print('clicked')
        if mouse_button == Qt.RightButton:
            self.swMap.setCanvasColor(Qt.Red)
        if mouse_button == Qt.LeftButton:
            self.swMap.setCanvasColor(Qt.Blue)

if __name__ == "__main__":
    print('start')

    app = QgsApplication([], True)
    app.initQgis()
    myapp = myMainClass()
    myapp.show()
    sys.exit(app.exec_())

The class main2.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(258, 137)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setMinimumSize(QtCore.QSize(0, 0))
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.mdiArea = QtWidgets.QMdiArea(self.centralwidget)
        self.mdiArea.setObjectName("mdiArea")
        self.verticalLayout_2.addWidget(self.mdiArea)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 258, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionOpenSubWin = QtWidgets.QAction(MainWindow)
        self.actionOpenSubWin.setObjectName("actionOpenSubWin")

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.actionOpenSubWin.setText(_translate("MainWindow", "OpenSubWin"))
2
  • 1
    I see that changeColor is missing "self" as the first parameter! Commented Apr 15, 2020 at 14:48
  • I edited the code, added the 'self', but the behaviour is exactly the same.
    – Javier G
    Commented Apr 16, 2020 at 5:48

1 Answer 1

2

the issue was on pointTool that needed to be part of the self

self.pointTool = QgsMapToolEmitPoint(self.swMap)
self.pointTool.canvasClicked.connect(self.changeColor)
self.swMap.setMapTool(self.pointTool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.