I've write a plugin that calls a tool when a certain push button in my tabifyDockWidget is clicked. When the comboBox button is clicked it works perfectly carring the comboBox.currentText()
to the tool, but when the lineEdit button is clicked the tool is called but doesnt get the lineEdit.text()
.
imports...
def run(self):
if not self.pluginIsActive:
self.pluginIsActive = True
if self.dockwidget == None:
self.dockwidget = revEnvDockWidget()
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
self.dockwidget.show()
# sets the text that must be passed to tool
# QComboBox
acquisitionTxt1 = self.dockwidget.acquisitionTab_comboBox.currentText()
editionTxt1 = self.dockwidget.editionTab_comboBox.currentText()
# QLineEdit
acquisitionTxt2 = self.dockwidget.acquisitionTab_lineEdit.text()
editionTxt2 = self.dockwidget.editionTab_lineEdit.text()
# sets tool, it's the same tool for each different text
a_ComboBoxClicked = HookCreator(self.iface.mapCanvas(), acquisitionTxt1)
a_lineEditClicked = HookCreator(self.iface.mapCanvas(), acquisitionTxt2)
e_ComboBoxClicked = HookCreator(self.iface.mapCanvas(), editionTxt1)
e_lineEditClicked = HookCreator(self.iface.mapCanvas(), editionTxt2)
# calls the tool when certain button is clicked
self.dockwidget.acquisitionTab_pushButton1.clicked.connect(
lambda: self.iface.mapCanvas().setMapTool(a_ComboBoxClicked))
self.dockwidget.acquisitionTab_pushButton2.clicked.connect(
lambda: self.iface.mapCanvas().setMapTool(a_lineEditClicked))
self.dockwidget.editionTab_pushButton1.clicked.connect(
lambda: self.iface.mapCanvas().setMapTool(e_ComboBoxClicked))
self.dockwidget.editionTab_pushButton2.clicked.connect(
lambda: self.iface.mapCanvas().setMapTool(e_lineEditClicked))
from qgis import core
from qgis.core import QgsProject, QgsFeature, QgsGeometry, QgsPointXY
from qgis.gui import QgsMapTool
from qgis.PyQt.QtCore import Qt
class HookCreator(QgsMapTool):
def __init__(self, canvas, txt):
self.canvas = canvas
QgsMapTool.__init__(self, self.canvas)
self.txt = txt
self.x = float
self.y = float
def canvasPressEvent(self, event):
point = self.toMapCoordinates(self.canvas.mouseLastXY())
self.x = point[0]
self.y = point[1]
def canvasReleaseEvent(self, event):
features = []
layer = QgsProject.instance().mapLayersByName('aux_revisao_p')[0]
pr = layer.dataProvider()
feature = core.QgsFeature(layer.fields())
geom = QgsGeometry().fromPointXY(QgsPointXY(self.x, self.y))
feature.setGeometry(geom)
features.append(feature)
for feat in features:
feat[1] = self.txt
feat[3] = 2
feat[4] = 999
feat[5] = 999
if event.button() == Qt.RightButton:
self.canvas.unsetMapTool(self)
else:
pr.addFeature(feat)
layer.commitChanges()
layer.triggerRepaint()
I've been searching about and find this question Activate a panel in tabbed panels that mentioned a findChild/Children method but I couldn't apply it. Am I missing somethig?
HookCreator
class since you are passing the line edit text to its constructor.