3

When writing a simple QGIS 3 plugin via Plugin Builder 3, is there a simple way to tell QGIS not to hide the dialog box when the mouse clicks outside it?

I have tried a few options without luck,
How to show QGIS plugin dialog always on top?, https://stackoverflow.com/questions/1925015/pyqt-always-on-top

Or is there an option within QGIS to command plugin displays?

Editing for including my code:

def __init__(self, iface):

    #always on top
    self.dlg = UpdateAttributeDialog()
    self.dlg.setWindowFlags(Qt.WindowStaysOnTopHint)

    """Constructor.

    :param iface: An interface instance that will be passed to this class
        which provides the hook by which you can manipulate the QGIS
        application at run time.
    :type iface: QgsInterface
    """
    # Save reference to the QGIS interface
    self.iface = iface

    # initialize plugin directory
    self.plugin_dir = os.path.dirname(__file__)
    # initialize locale
    locale = QSettings().value('locale/userLocale')[0:2]
    locale_path = os.path.join(
        self.plugin_dir,
        'i18n',
        'UpdateAttribute_{}.qm'.format(locale))

    if os.path.exists(locale_path):
        self.translator = QTranslator()
        self.translator.load(locale_path)

        if qVersion() > '4.3.3':
            QCoreApplication.installTranslator(self.translator)

    # Declare instance attributes
    self.actions = []
    self.menu = self.tr(u'&Update Attribute')

    # Check if plugin was started the first time in current QGIS session
    # Must be set in initGui() to survive plugin reloads
    self.first_start = None

UPDATE

The two suggested lines just needed to be moved to the run(self) section. Replace 'UpdateAttributeDialog()' by the name of your dialog.

 def run(self):
    """Run method that performs all the real work"""

    # Create the dialog with elements (after translation) and keep reference
    # Only create GUI ONCE in callback, so that it will only load when the plugin is started
    #if self.first_start == True:
    #    self.first_start = False
    self.dlg = UpdateAttributeDialog()
    self.dlg.setWindowFlags(Qt.WindowStaysOnTopHint)
  • Since then I found using dockwidgets instead of dialog to be perfect. The always on top option can be annoying when dealing with progress bars and extra windows. – Leo Feb 17 at 22:01
5

Yes, there are a simple way. I found out one for QGIS 3 and, I think that is also equivalent for QGIS 2 because is based in 'setWindowFlags' method for QDialog (see this answer). For QGIS 3, it's necessary to import Qt module from PyQt5.QtCore and to include one instruction in init method; as is pointed out in snippet code below:

.
.
.
from PyQt5.QtCore import Qt
.
.
.
    def __init__(self, iface):
.
.
.
        # Create the dialog (after translation) and keep reference
        self.dlg = WhereAmIDialog()
        self.dlg.setWindowFlags(Qt.WindowStaysOnTopHint)
.
.
.

On the other hand, there is an option within QGIS to command plugin displays 'Always On Top'. With cursor on plugin window dialog (bottom bar), click on mouse right button and mark 'Always On Top' option in contextual menu; as it can be observed at below image.

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.