4

I'm trying to create a mandatory QgsFileWidget in a QWizard form for a plugin but the result is a Next button that doesn't get active when I choose the path to save the project. Here is the snippet of the code:

import os

from PyQt5.QtWidgets import QWizard, QWizardPage
from qgis.PyQt import uic

FORM_CLASS, _ = uic.loadUiType(os.path.join(
    os.path.dirname(__file__), 'plugin_wizard_base.ui'))

class PluginDialog(QWizard, QWizardPage, FORM_CLASS):
    def __init__(self, parent=None):
        """Constructor."""
        super(PluginDialog, self).__init__(parent)
        self.setupUi(self)
        try:
            self.wizardPage.registerField('projPath*', self.projPath, self.projPath.filePath(), 'fileChanged')
        except TypeError:
            self.wizardPage.registerField('projPath*', self.projPath, self.projPath.filePath(), self.projPath.fileChanged)


This code works with a QLineEdit perfectly, the only difference is the method to retrieve the path string and the signal (.text() instead .filePath() and .textChanged instead .fileChanged).

I've tried searching in the documentation but I didn't find an example specifically for QgsFileWidget. I feel that is a minor detail I've been missing. Any suggestions?

I'm using PyQt5 with Qt Designer to generate the GUI for the plugin.

Update:

I replaced the code as follows:

        ...
        self.setupUi(self)
        self.wizardPage.registerField('projPath*', self.projPath, 'filePath', self.projPath.fileChanged)
        self.projPath.fileChanged.connect(self.file_changed)

    def file_changed(self):
        self.setField("projPath", self.projPath.filePath())

    def isComplete(self):  # Re-implementation of isComplete
        return bool(self.projPath.filePath())

But the result was that the next button get unlocked only if I type the path directly in the text field. It doesn't work if I choose a path in the explorer dialog. Even so, after unlock the button, if I clean the field, the button remain active.

1
  • 1
    Could you provide a working example? Commented Jan 10, 2022 at 9:52

1 Answer 1

4

Try to pass the third parameter as string which refers property name, filePath here.

self.wizardPage.registerField('projPath*', self.projPath, 'filePath', self.projPath.fileChanged)

Here is a working example in QGIS Python Editor:

from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *


class Wizard(QWizard):
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)
        self.addPage(Page1(self))
        self.addPage(Page2(self))
        self.setWindowTitle("Test")


class Page1(QWizardPage):
    def __init__(self, parent=None):
        super(Page1, self).__init__(parent)

        self.projPath = QgsFileWidget(self)
        self.registerField('projPath*', self.projPath, 'filePath', self.projPath.fileChanged)
        self.projPath.fileChanged.connect(self.file_changed)
        
        layout = QVBoxLayout()
        layout.addWidget(self.projPath)
        self.setLayout(layout)
        
    def file_changed(self):
        self.setField("projPath", self.projPath.filePath())
    
    def isComplete(self): # Re-implementation of isComplete
        return bool(self.projPath.filePath())


class Page2(QWizardPage):
    def __init__(self, parent=None):
        super(Page2, self).__init__(parent)
        self.label = QLabel()
        layout = QVBoxLayout()
        layout.addWidget(self.label)
        self.setLayout(layout)
    
    def initializePage(self):
        path = self.field("projPath")
        self.label.setText(f"Path: '{path}'")
        

wizard = Wizard()
wizard.show()

enter image description here

1
  • I replaced the code as the example but with modifications because I'm using a .ui file from QT Designer to assemble the GUI of the plugin: self.setupUi(self) self.wizardPage.registerField('projPath*', self.projPath, 'filePath', self.projPath.fileChanged) I don't know if this Wizard Pages structure is affecting the field registering but the result was that the next button get unlocked only if I type the path directly in the text field. It doesn't work if I choose a path in the explorer dialog. Even so, after unlock the button, if I clean the field, the button remain active. Commented Apr 6, 2022 at 23:06

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.