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()
load_shapefiles
function from the button inside this new dialog ? Update your question with more precise information.