5

On Windows, if I open a project in QGIS then the project file name appears in the title bar.

If I do the same thing in PyQGIS using the following code the title bar does not change.

from PyQt4.QtCore import QFileInfo
QgsProject.instance().read(QFileInfo("C:/FilePath.qgs"))

But the project loads fine, I can see my project data and if I click the save button then the file name appears in the title bar.

I was wondering if there was a bridge or some hack needed to get the file name to appear. I need this because my client has similar looking projects and uses the file name to know that my code has loaded the right one for them. Of course I could show it to them another way like in the message bar but seeing as opened projects normally get their file name in the title bar I thought I should be keeping to this 'standard' way of doing it.

Things I've tried...

  1. Saving the file via:

    QgsProject.instance().write()
    

Does save the file but doesn't update the title bar.

  1. Canvas bridge code

    from qgis.gui import *
    canvas = QgsMapCanvas()
    
    bridge = QgsLayerTreeMapCanvasBridge( \
         QgsProject.instance().layerTreeRoot(), canvas)
    

Also didn't help.

How can I get the file name to appear in the title bar of QGIS when using .read() ?

2

Normally in the titlebar, it will read QGIS 2.18.2 (or whatever version being used).

What I tend to do is to remove this and instead just use the name of the project that's loaded. So instead of reading:

QGIS 2.18.2 - MyProject

it will simply read:

MyProject

Personal choice but you can edit the title using:

from PyQt4.QtCore import QFileInfo
import os

project_path = "C:/FilePath.qgs"

QgsProject.instance().read(QFileInfo(project_path))
iface.mainWindow().setWindowTitle(os.path.splitext(os.path.basename(project_path))[0])
#iface.mainWindow().setWindowTitle("QGIS 2.18.2 - " + os.path.splitext(os.path.basename(project_path))[0])

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.