5

I use Plugin Builder and Qt Designer to create a plugin on QGIS.

I would like to export a PDF file with results of processing. I try to use QgsFileWidget, to get the directory, but I cannot connect the plugin_dialog_base.ui file on plugin.py, to take the user directory to the 'OUTPUT': line for export the PDF file.

How do I connect QgFileWiget in the interface?

enter image description here

2 Answers 2

7

Plugin Builder adds a method named run to the plugin.py. You can implement the script below with a little editing. I assume you have set storageMode to QgsFileWidget.StorageMode.GetDirectory for QgsFileWidget and named localsave in Qt Designer.

def run(self):

    if self.first_start == True:
        self.first_start = False
        self.dlg = testDialog() # Change into your dialog name
        
    self.dlg.show()
    result = self.dlg.exec_()

    if result:
       
        folder_path = self.dlg.localsave.filePath()  # Get folder path from QgsFileWidget
        
        DATANAME = datetime.now().strftime("%d_%m_%y")
        full_path = os.path.join(folder_path, DATANAME + '.pdf' )
        
        print(full_path)

Now, you can use full_path in processing.run.

enter image description here

5
  • I recive this mensage error >>> AttributeError: 'QgsFileWidget' object has no attribute 'filepath' Traceback (most recent call last): File "C:/Users/Avell/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\polo\POLO.py", line 673, in run folder_path = self.dlg.localsave.filepath() AttributeError: 'QgsFileWidget' object has no attribute 'filepath' Apr 21, 2021 at 18:08
  • 1
    @NewtonMonteiro Python is a case-sensistive language. Use filePath (Capital P) instead of filepath. Apr 21, 2021 at 18:12
  • thanks a lot, problem finally solved, I search for this anser a few months!! tks tks tks!!!! Apr 21, 2021 at 20:57
  • one more question, I would like the code run only after answer the local to save PDF file and click on OK button, but the code runs anyway click on cancel button or close the window, how fix that Apr 21, 2021 at 23:14
  • @NewtonMonteiro It is difficult for me to say anything without seeing your actual code. You can ask that in a separate post including a sample script. Apr 21, 2021 at 23:21
1

Use os.path.join(folder,file) instead of concatenation. You have no path separator

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.