1

I am trying to change the project's title in a Python macro. But there is a mistake I cannot solve:

AttributeError: 'str' object has no attribute 'setTitle';.

Can you help me?

This is the code I am using:

from qgis.utils import iface
from qgis.core import QgsProject

def saveProject():

title = QgsProject.instance().title()

 
title.setTitle("HOLAAA")
New contributor
Tania is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    Hi Tania, welcome to GIS Stack Exchange. I edited your question, but in future, please don't use all caps in your question titles- it tends to attract a lot of down votes.
    – Ben W
    Sep 23 at 12:29

1 Answer 1

1

The setTitle() method belongs to the QgsProject class, but you are trying to call it on the string object which is returned when you retrieve the current title.

As per the docs, this method provides:

a shortcut to setting the title in the project's metadata()

So, to set the title in the project's metadata, you can just do:

project = QgsProject.instance()
project.setTitle('Holaaa')
project.write()

This will not rename the .qgz project file. To also do that at the same time, you would need to do something like:

import os

project = QgsProject.instance()
project.setTitle('Holaaa')
project.write()

old_file = project.fileName()
new_file = os.path.join(project.homePath(), 'Holaaa.qgz')
os.rename(old_file, new_file)
2

Your Answer

Tania is a new contributor. Be nice, and check out our Code of Conduct.

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.