Geographic Information Systems Asked by BallpenMan on June 8, 2021
I’m making this plugin and it’s big as seen in the screenshot below
However, when I open it in QGIS, it’s small and it’s troublesome to keep adjusting the size.
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:
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:UsersBenDesktopguilatlongcalc_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.
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:UsersBenDesktopguilatlongcalc_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.
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:
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:
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.
Answered by Ben W on June 8, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP