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_()
2
  • Does it mention what line the error is originating from?
    – Joseph
    Jun 25, 2019 at 10:20
  • 2
    The fourth line - self.dlg.comboBox.currentTextChanged(filename)
    – E_123
    Jun 25, 2019 at 10:25

1 Answer 1

2

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)
2
  • 1
    Thank you. This worked. Now I am getting a better understanding.
    – E_123
    Jul 1, 2019 at 6:48
  • @B_123 - Most welcome, glad it helped :)
    – Joseph
    Jul 1, 2019 at 8:56

Your Answer

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

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