1

I am trying to develop my first simple QGIS plugin, which just loads all shapefiles from a folder into the current project by opening a window and click there a button "import". the importing does work, if I click the thumbnail of the plugin in the QGIS toolbar. But it does not open the user interface with my created button in qt designer. What am I doing wrong?

import os
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProject, QgsVectorLayer
from PyQt5.QtWidgets import QAction
from PyQt5.QtGui import QIcon


def load_shapefiles():
    path = "C:/Users/Giaco/Desktop/windkraft"
    for file_name in os.listdir(path):
        if file_name.endswith(".shp"):
            layer_name = os.path.splitext(file_name)[0]
            layer_path = os.path.join(path, file_name)
            layer = QgsVectorLayer(layer_path, layer_name, "ogr")
            if not layer.isValid():
                print("Layer failed to load: " + layer_name)
            QgsProject.instance().addMapLayer(layer)
    print("Shapefiles loaded successfully")

class LoadShapefiles:
    def __init__(self, iface):
        self.iface = iface

    def initGui(self):
        # Create action that will start plugin configuration
        self.action = self.create_action(
            "Load Shapefiles",
            "icon.png",
            "Load shapefiles into current project",
            self.run)

        # Add toolbar button and menu item
        self.iface.addToolBarIcon(self.action)
        self.iface.addPluginToMenu("&Load Shapefiles", self.action)

    def create_action(self, text, icon, description, function):
        action = QAction(QCoreApplication.translate("LoadShapefiles", text), self.iface.mainWindow())
        action.setIcon(QIcon(os.path.join(os.path.dirname(__file__), icon)))
        action.setToolTip(QCoreApplication.translate("LoadShapefiles", description))
        action.triggered.connect(function)
        return action

    def unload(self):
        self.iface.removeToolBarIcon(self.action)
        self.iface.removePluginMenu("&Load Shapefiles", self.action)

    def run(self):
        load_shapefiles()


2
  • "But it does not open the user interface with my created button in qt designer", there isn't code for loading a QtDesigner file. so I'm not sure to understand. If I understand correctly, you want to open a new dialog from the toolbar, and then launch the load_shapefiles function from the button inside this new dialog ? Update your question with more precise information.
    – etrimaille
    Commented May 12, 2023 at 20:15
  • 1
    Do not forget that all plugins source code is on your computer, you can always install a small one, having similar features, and then inspect its own source code. It's the best way to learn (well, sometimes, some plugins are not following good practice, but still, it's a beginning)
    – etrimaille
    Commented May 12, 2023 at 20:17

2 Answers 2

0

Working as written. I do not see any definition of an interface to open. I do not see any attempts to open an interface.

But it does not open the user interface with my created button in qt designer. What am I doing wrong?

To open an interface you would want to add calls to the run function or something that run calls. The following is one possibility.

def run(self):
  self.my_window = MyWidget()
  self.my_window.show()
  # Run the dialog event loop
  result = self.my_window.exec_()

With a minimal example of use of the .ui file.

import os
from PyQt5 import uic
import PyQt5.QtWidgets as qtw
DESIGNER, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'my_widget.ui'))

class MyWidget(qtw.QDialog,DESIGNER):
  def __init__(self):
    super(MyWidget, self).__init__(None)
        self.setupUi(self)
0

There is a plugin called "plugin builder" which will create boilerplate plugin code that works.

The documentation is at http://g-sherman.github.io/Qgis-Plugin-Builder/ and the "plugin builder" plugin can be installed from the plugin repository in QGIS.

This does not answer your question directly, but may be very helpful.

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.