2

I am creating a QGIS plugin. I have added a combo box and a push button in the plugin. The issue that I am facing is that I want the ComboBox to either allow selection of layers from the layers loaded on the project or when the user clicks the pushButton it should navigate to the directory and the ComboBox should be filled with the layer selected from the pushButton. Both work individually, but I can't seem to get them working together. I am a beginner in Python, so please let me know if there is anything very obvious that I have missed out on. I am giving my code below. When I run it I get the error -

TypeError: native Qt signal is not callable

def select_input_raster(self):
  filename, _filter = QFileDialog.getOpenFileName(
    self.dlg, "Select input raster ","", '*.tif')
  self.dlg.comboBox.currentTextChanged(filename)

def run(self):

    if self.first_start == True:
        self.first_start = False
        self.dlg = ComboBoxPushButtonDialog()
        self.dlg.pushButton.clicked.connect(self.select_input_raster)


    layerlist1 = ['<Select a raster layer>']
    layers1 = QgsProject.instance().mapLayers().values()
    for layer in layers1:
        if layer.type() == QgsMapLayer.RasterLayer:
            layerlist1.append(layer.name())

        #else layerlist1.append(select_input_raster())

    #Clear the contents of the comboBox from previous runs
    self.dlg.comboBox.clear()
    # Populate the comboBox with names of all the loaded raster layers
    self.dlg.comboBox.addItems(layerlist1)
    # show the dialog
    self.dlg.show()
    # Run the dialog event loop
    result = self.dlg.exec_()
  • Does it mention what line the error is originating from? – Joseph Jun 25 at 10:20
  • 2
    The fourth line - self.dlg.comboBox.currentTextChanged(filename) – B_123 Jun 25 at 10:25
1

currentTextChanged() is a signal which needs to be connected to a function. I.e.

self.dlg.comboBox.currentTextChanged.connect(self.some_function)

I think what you want is something like:

def select_input_raster(self):
    filename, _filter = QFileDialog.getOpenFileName(
      self.dlg, "Select input raster ","", '*.tif')
    # Add the selected filename to combobox
    self.dlg.comboBox.addItem(filename)
    # Obtain index of newly-added item
    index = self.dlg.comboBox.findText(filename)
    # Set the combobox to select the new item
    self.dlg.comboBox.setCurrentIndex(index)
  • 1
    Thank you. This worked. Now I am getting a better understanding. – B_123 Jul 1 at 6:48
  • @B_123 - Most welcome, glad it helped :) – Joseph Jul 1 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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