Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created February 28, 2021 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/45323e31d814bb9b45e6d07fb730467e to your computer and use it in GitHub Desktop.
Save ThomasG77/45323e31d814bb9b45e6d07fb730467e to your computer and use it in GitHub Desktop.
Just some ideas to learn about refactoring using functions
# Before
# Crédit
credit_text2 = QgsLayoutItemLabel(layout)
credit_text2.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text2.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text2.setHAlign(Qt.AlignLeft)
credit_text2.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text2.attemptMove(QgsLayoutPoint(6, 268.737),page=1)
credit_text3 = QgsLayoutItemLabel(layout)
credit_text3.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text3.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text3.setHAlign(Qt.AlignLeft)
credit_text3.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text3.attemptMove(QgsLayoutPoint(6, 268.737),page=2)
# After (could be largely improved, jus to give principles e.g reusing code with functions
def generateLayoutItem(layout, page)
credit_text = QgsLayoutItemLabel(layout)
credit_text.setText("Plan réalisé par le Club agroenvironnemental de l'Estrie")
credit_text.setFont(QFont("Ms Shell Dlg 2", 11))
credit_text.setHAlign(Qt.AlignLeft)
credit_text.setMinimumSize(QgsLayoutSize(133.376, 8.215, QgsUnitTypes.LayoutMillimeters))
credit_text.attemptMove(QgsLayoutPoint(6, 268.737),page=page)
return credit_text
credit_text2 = generateLayoutItem(layout, 1)
credit_text3 = generateLayoutItem(layout, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment