4

I am using QGIS 3.10.0 and Windows 10. I have a project where I want the user to define custom project variables. I want to make this user friendly, I don't want the user to need to open the project settings.

So, I'm looking for a way where upon loading a project, an input dialog pops up in which the user can set custom project variables.

I've been trying to create a project macro and using PyQt5 to load an input dialog, but I keep getting error messages. I'm not a Python programmer, so I'm not even sure what I'm doing...

Could someone help me out and give me a hint on how to proceed?

EDIT So I've added the code below to my project macro. Now whenever I launch the project, I get three seperate input dialogs. It works, but I would rather have one single dialog with three text fields. I know I have to use QDialog instead of QInputDialog, but I don't get it to work...

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon
from qgis.core import QgsExpressionContextUtils, QgsProject

project = QgsProject.instance()

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'Scherm'
        self.left = 10
        self.top = 10
        self.width = 640
        self.height = 480
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.getToponiem()
        self.getOpdrachtgever()
        self.getProjectnummer()

        self.show()

    def getToponiem(self):
        text, okPressed = QInputDialog.getText(self, "Toponiem","Toponiem:", QLineEdit.Normal, "")
        if okPressed and text != '':
            QgsExpressionContextUtils.setProjectVariable(project,'Toponiem',text)

    def getOpdrachtgever(self):
        text, okPressed = QInputDialog.getText(self, "Opdrachtgever","Opdrachtgever:", QLineEdit.Normal, "")
        if okPressed and text != '':
            QgsExpressionContextUtils.setProjectVariable(project,'Opdrachtgever',text)

    def getProjectnummer(self):
        i, okPressed = QInputDialog.getInt(self, "Projectnummer","Projectnummer:", 0, 0, 1000000, 1)
        if okPressed:
            QgsExpressionContextUtils.setProjectVariable(project,'Projectnummer',i)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

def openProject():
    App()

def saveProject():
    pass

def closeProject():
    pass

EDIT2 I've been trying to call an QDialog when a project opens, but it doesn't work properly. The very simple code below does get executed but the dialog window opens and immediately closes again. How can I keep the dialog open until the user presses the button? (I will later fill the dialog with more options for the user to fill in).

from PyQt5.QtWidgets import QWidget, QPushButton

def openProject():
    w = QWidget()
    w.resize(500,500)
    button = QPushButton('OK')
    button.resize(200,32)
    button.move(80, 200)
    w.show()
  • I suggest you specify what you mean by "custom project variables". Is it just a name, or 15 popup-boxes, or what? – Erik Dec 16 '19 at 15:23
  • I'm talking about 3 different project variables (see my edit above). – Geert Dec 16 '19 at 16:09
  • So can you share any code where you get stuck with QDialog? – Andreas Müller Dec 16 '19 at 23:06
  • @AndreasMüller I've edited my post to demonstrate the issue. – Geert Dec 22 '19 at 15:52
  • You don't use QDialog in your example. Write a class by subclassing QDialog and insert the widgets you need. Then call this Dialog and its exec_-loop. There are excellent examples in the qt-documentation. – Andreas Müller Dec 23 '19 at 12:59

Your Answer

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

Browse other questions tagged or ask your own question.