1

I'm making this plugin and it's big as seen in the screenshot below enter image description here

However, when I open it in QGIS, it's small and it's troublesome to keep adjusting the size. enter image description here

5
  • 1
    You can set minimum sizes for each widget. You have 0 × 0 for the spin box settings in your screen shot. Also, I strongly recommend the use of Qt layouts e.g. look at QGridLayout.
    – Ben W
    Jun 7 at 13:01
  • How can I know what's the minimum size for each widget? it seems to be problematic that the size in the design window isn't equal to the size in QGIS.
    – BallpenMan
    Jun 7 at 13:09
  • Look at the geometry attribute in your Qt Designer screenshot. Your spin box is 101 × 31. If that's really the size you want, make that the minimum size. But dragging, dropping and resizing without setting a layout is not the way to achieve consistent ui appearance. Honestly, I think you would benefit from checking out some YouTube vids/ tutorials on using Qt Designer.
    – Ben W
    Jun 7 at 13:41
  • I've set every object to its minimum size and the plugin still comes out small in QGIS. Well, this is actually part of an online course but my QGIS' behavior is different to the instructors. No effect on the plugin's actual usability, the size is just wonky in QGIS.
    – BallpenMan
    Jun 7 at 13:56
  • 1
    Things like screen size can make a difference too. For this reason I really do urge you to look into using the different layout types available in Qt. Using layouts as well as other things like stretch, spacers etc can alleviate many of these issues.
    – Ben W
    Jun 7 at 14:12
5

Simply dragging and dropping widgets onto a dialog in Qt Designer is a quick way to get a working ui, but it does not in any way guarantee consistent appearance. To demonstrate this, I created a dummy dialog similar to yours. This is how it looks in Qt Designer:

enter image description here

I then opened it in QGIS with the following code run from the Python console:

from PyQt5 import uic
from PyQt5.QtWidgets import QDialog

FORM_CLASS, _ = uic.loadUiType('C:\\Users\\Ben\\Desktop\\gui\\latlongcalc_dialog_base.ui')

class TestDialog(QDialog, FORM_CLASS):
    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)

dlg = TestDialog()
dlg.show()

The appearance and behaviour can be seen below. You can see that the labels are not displayed properly, the dialog is small and nothing responds to resizing of the window.

enter image description here

Incidentally, this has nothing to do with QGIS. The result is exactly the same after setting the environment with a batch file, creating a QApplication and launching the dialog from the command line with the following code:

import sys
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QDialog

FORM_CLASS, _ = uic.loadUiType('C:\\Users\\Ben\\Desktop\\gui\\latlongcalc_dialog_base.ui')

class TestDialog(QDialog, FORM_CLASS):
    def __init__(self):
        QDialog.__init__(self)
        self.setupUi(self)

app = QApplication([])
dlg = TestDialog()
dlg.show()
sys.exit(app.exec_())

We haven't told Qt how to display any of the widgets we are using. This is where layouts come in. Let's make a couple of improvements. Open the .ui file in Qt Designer, right click in the dialog and select Lay out > Lay Out in a Grid.

enter image description here

After opening again in QGIS, you can see that now the labels are shown properly and that all the widgets respond when the dialog is resized:

enter image description here

If you want to set the size of the dialog window itself or its child widgets, there are a couple of options. You can set these properties directly in Qt Designer or edit the plugin code (my preferred method), using methods such as setGeometry() and setMinimumSize(). For example, if you add the following lines inside the initGui() method of your main plugin file (you may also need to add QSpinBox to the imports from PyQt5.QtWidgets):

self.dlg.setGeometry(550, 250, 750, 500)
for sb in self.dlg.findChildren(QSpinBox):
    sb.setMinimumSize(150, 50)

You should see the following:

enter image description here

Experiment with passing different argument values. The method setGeometry() takes four integer arguments- the first two are x and y values for the location of the top left corner of a window or dialog (not including the title bar) and the second two are width & height.

This is just an example, you need to do your own learning to really explore both Qt Designer as well as the methods available in the Qt API. The Qt documentation is generally excellent, and there are plenty of videos on Youtube about PyQt gui programming.

2
  • 1
    Thank you! The plugin is now in the right size.
    – BallpenMan
    Jun 9 at 13:10
  • No problem! Glad it was helpful.
    – Ben W
    Jun 9 at 13:38

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.