2

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)
  • I'd rather see your code as text pasted and formatted than as a screen shot, it makes it easier to copy/paste to try out the code you've presented to replicate the problem you're experiencing. – Michael Stimson Jan 14 at 4:36
  • 1
    Please erase your two added lines below #always on top. Find out this line 'Create the dialog (after translation) and keep reference'. Below self.dlg.AttributeDialog place self.dlg.setWindowFlags(Qt.WindowStaysOnTopHint). – xunilk Jan 14 at 4:38
  • @Michael Stimson I had to see that image to find out the issue. Two lines were placed in a wrong way. – xunilk Jan 14 at 4:41
  • @xunilk Hooray! It's working! Thank you so much for your help, you've been amazing! – Leo Jan 14 at 4:49
  • Good for you. Please, consider also mark the question as accepted. Thanks. – xunilk Jan 14 at 5:04
3

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

  • Thank you @xunilk for the quick answer. It doesn't work :( Qt has been added to the import list, it's added in def __init__(self, iface): nothing sadly. Is there another line to add maybe ? – Leo Jan 14 at 3:44
  • Windows doesn't allow the 'Always on top feature' – Leo Jan 14 at 3:46
  • It is 'init' method of main plugin file (not init.py file of plugin). It works perfectly in my Windows 10. – xunilk Jan 14 at 4:03
  • By the way, what windows do you use? – xunilk Jan 14 at 4:06
  • Yes I have updated the init method part of the main plugin file. Yes Windows 10 too. – Leo Jan 14 at 4:10

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.