set application name in pyside

2019-03-02 11:51发布

问题:

I created an Application using Qt Creator/Designer under Windows 8 and Qt 5

it starts as follow

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #MainWindow.setApplicationName("Facturo-Pro") # this doesn't work
        MainWindow.setWindowIcon(QtGui.QIcon('icons/app.png'))
        MainWindow.setObjectName("MainWindow")
        MainWindow.setMinimumSize(QtCore.QSize(800, 600))
        MainWindow.setLocale(QtCore.QLocale(QtCore.QLocale.English, QtCore.QLocale.UnitedStates))

i want to set the application name which should be shown on the window title and task bar

i tried to use

    QtCore.QCoreApplication.setOrganizationName("Moose Soft")
    QtCore.QCoreApplication.setApplicationName("Facturo-Pro")

or

QtCore.QSettings("my app","my org")

but it didn't work, in task bar and window title i see "python"

i don't want to use setWindowTitle() because i want to use

    MainWindow.setWindowFilePath(self.currentFile)

so i will later just update the FilePath and when editing the it a "*" will be shown on the window title!

回答1:

With Qt5, you can set the applicationDisplayName, which is separate from the main title-bar text.

To show the modification state in the title-bar, you would do this:

    QtWidget.qApp.setApplicationDisplayName('Test')
    ...
    window.setWindowFilePath('/path/to/file.txt')
    window.setWindowModified(True)

and the title-bar would look like this: file.txt* - Test

Alternatively, you can get a little more control of the title-bar text by using a special placeholder when setting the window title:

    window.setWindowTitle('/path/to/file.txt[*]')
    window.setWindowModified(True)

and the title-bar would look like this: /path/to/file.txt* - Test

EDIT:

If you're using Qt4, there will no be applicationDisplayName, so you could try this, instead:

    QtGui.qApp.setApplicationName('Test')
    ...
    window.setWindowTitle(
        '/path/to/file.txt[*] - %s' % QtGui.qApp.applicationName())
    window.setWindowModified(True)

and the title-bar should look like this: /path/to/file.txt* - Test