TransWikia.com

How to run Python script in QGIS via plugin

Geographic Information Systems Asked on March 12, 2021

I am an absolute beginner in QGIS no idea in this field.
I just created a Python plugin with pluging builder in QGIS successfully

PYTHON_PLUGINS_PATH/ MyPlugin/ —————– inside it contain these files

       ____init__.py  
       mainPlugin.py  
       mainPlugin_dialog.py
       mainPlugin_dialog_base.ui
       plugin_upload.py
       README.txt
       metadata.txt  
       resources.qrc  
       resources.py
       resources_rc.py
       .
       .
       etc

I just want to write some code like print(“hello world”).
I want to know should I write it inside the mainPlugin.py file If yes then ok, otherwise tell me where should write my code.

After writing my code how can I run It through my plugin which I created
in QGIS. I am seeing only my plugin name in QGIS my plugin has no option to import file to take this file mainPlugin.py and run it.

What is the procedure to run my mainPlugin.py file through my plugin inside QGIS?

2 Answers

If you have not already pip install pb_tool Get to the directory with your pb_tool.cfg file. You may have to modify the path: inside the pb_tool.cfg to point to your /plugins directory in QGIS3.4.

Then open your directory with your pb_tool.cfg file from the cmd line and type pb_tool deploy.

If that goes well you should have a new plugin in QGIS that will open a blank dialog.

What's Next From your README.html file - some of this is redundant if you have already ran pb_tool deploy

  1. In your plugin directory, compile the resources file using pyrcc5
    (simply run make if you have automake or use pb_tool)
  2. Test the generated sources using make test (or run tests from your IDE) Copy the entire directory containing your new plugin to the QGIS plugin directory (see Notes below)
  3. Test the plugin by enabling it in the QGIS plugin manager
  4. Customize it by editing the implementation file MainPlugin.py
  5. Modify your user interface by opening MainPlugin.ui in Qt Designer
  6. Create your own custom icon, replacing the default icon.png

Answered by Cary H on March 12, 2021

I have a video that covers the v3x plugin deployment process.

https://www.youtube.com/watch?v=NqkP_fCH098

Once you get the plugin created and complied you can add a hello world print statement after the .show() method in the mainPlugin.py file. The normal print statement will not show since the plugin is not directly interacting with the python console so I'd recommend you write to the PyQt5 QMessageBox() method.

Answered by artwork21 on March 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP