2

Plugin Intro:

I am creating a plugin for in-house development which lists the location of vector/raster layers of specific projects. Each project has a number of files ranging from 0 to few hundreds. The preface of the plugin is, I need to type the project ID and click a button which fetches the list of files (from an in-house database) in the form of table widget. When I click a specific row and click another button the files from the row clicked are opened in QGIS.

Problem:

When the number of files is one, the plugin works perfectly. The problem arises when the number of files are more than one. In the latter case when I click a row the same file is loaded several times. There is no loop in my code so I don't understand why this is happening. The code is pasted below.

Function to retrieve data and show in table widget:

def ListPage(self):
    self.tableWidget.clear() 
    self.cid = self.textEdit.toPlainText() #to get the number of project id #
    self.data = prj.getProductsByStatusAndOutput(status='READY_TO_CLOUDMASK', output_id=self.cid)
    self.tableWidget.setHorizontalHeaderLabels (('Id','File Name','Status', 'Location'))
    header = self.tableWidget.horizontalHeader()       
    header.setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(1, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(2, QtWidgets.QHeaderView.ResizeToContents)
    header.setSectionResizeMode(3, QtWidgets.QHeaderView.ResizeToContents)



    for rowIndex, record in enumerate(self.data):
        itemId = QTableWidgetItem(record['identifier'])
        itemStatus = QTableWidgetItem(record['status'])
        itemFilename = QTableWidgetItem(record['filename'])
        itemloc = QTableWidgetItem(record['prod_location'])

        self.tableWidget.setItem( rowIndex, 0, itemId)
        self.tableWidget.setItem( rowIndex, 1, itemFilename)  
        self.tableWidget.setItem( rowIndex, 2, itemStatus)  
        self.tableWidget.setItem( rowIndex, 3, itemloc)

        self.pushButton_2.clicked.connect(self.OpenQgis)

Function to open the files when a row is clicked:

def OpenQgis(self):


    row = self.tableWidget.currentRow()
    path = (self.tableWidget.item(row,3).text())
    sname = (self.tableWidget.item(row,1).text())
    vlocation = path + "/" + sname
    tname = (os.path.basename(vlocation)[0:28]+"_stack.tif")
    tfname = (os.path.basename(vlocation)[0:28]+"_stack")
    rlocation = path+ "/" +tname
    self.textEdit_3.setText(tname)
    if os.path.exists(vlocation):
        iface.addVectorLayer (vlocation,"","ogr")
    else:
        QMessageBox.about(self, "Error", "Invalid shape file location")
    if os.path.exists(rlocation):
        iface.addRasterLayer (rlocation,tfname,"gdal")
    else:
        QMessageBox.about(self, "Error", "Invalid raster file location")  
5
  • Did you click or double-click on your tableWidget row ? Feb 5, 2020 at 15:40
  • 1
    Move self.pushButton_2.clicked.connect(self.OpenQgis) outside the loop otherwise you are attaching to the button multiple events. It causes your "only" click to emit multiple calls and hence add multiple layers. Could be wrong: untested
    – ThomasG77
    Feb 5, 2020 at 15:42
  • @ J. Monticolo I clicked the row once and click the second button. Basically I have to select the row and click the second button to open the files.
    – lawr
    Feb 5, 2020 at 15:42
  • @ThomasG77 Thank you! That definitely was the problem and I have been looking at the code for three days now. You can change it as an answer if you wish.
    – lawr
    Feb 5, 2020 at 15:53
  • "Official" answer done
    – ThomasG77
    Feb 5, 2020 at 15:57

1 Answer 1

1

Move self.pushButton_2.clicked.connect(self.OpenQgis) outside the loop otherwise you are attaching to the button multiple events.

It causes your "only" click to emit multiple calls and hence add multiple layers.

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.