Don't overwrite applicationName if already set.

My commit 6c973dee2c broke the case where setApplicationName
is called before the QCoreApplication constructor.
Fixed and added autotest.

Task-number: QTBUG-45283
Change-Id: If7bdb0d82be50b50a95a04027f5f9d7143c1a7ac
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
David Faure 2015-03-29 22:09:56 +02:00
parent 1ac1ae05f5
commit f1bfc4266b
2 changed files with 22 additions and 4 deletions

View File

@ -326,6 +326,7 @@ struct QCoreApplicationData {
#ifndef QT_NO_LIBRARY #ifndef QT_NO_LIBRARY
app_libpaths = 0; app_libpaths = 0;
#endif #endif
applicationNameSet = false;
} }
~QCoreApplicationData() { ~QCoreApplicationData() {
#ifndef QT_NO_LIBRARY #ifndef QT_NO_LIBRARY
@ -370,8 +371,8 @@ struct QCoreApplicationData {
QString orgName, orgDomain; QString orgName, orgDomain;
QString application; // application name, initially from argv[0], can then be modified. QString application; // application name, initially from argv[0], can then be modified.
QString applicationNameCompat; // for QDesktopServices. Only set explicitly.
QString applicationVersion; QString applicationVersion;
bool applicationNameSet; // true if setApplicationName was called
#ifndef QT_NO_LIBRARY #ifndef QT_NO_LIBRARY
QStringList *app_libpaths; QStringList *app_libpaths;
@ -753,7 +754,8 @@ void QCoreApplication::init()
QCoreApplication::self = this; QCoreApplication::self = this;
// Store app name (so it's still available after QCoreApplication is destroyed) // Store app name (so it's still available after QCoreApplication is destroyed)
coreappdata()->application = d_func()->appName(); if (!coreappdata()->applicationNameSet)
coreappdata()->application = d_func()->appName();
QLoggingRegistry::instance()->init(); QLoggingRegistry::instance()->init();
@ -2350,13 +2352,13 @@ QString QCoreApplication::organizationDomain()
*/ */
void QCoreApplication::setApplicationName(const QString &application) void QCoreApplication::setApplicationName(const QString &application)
{ {
coreappdata()->applicationNameSet = !application.isEmpty();
QString newAppName = application; QString newAppName = application;
if (newAppName.isEmpty() && QCoreApplication::self) if (newAppName.isEmpty() && QCoreApplication::self)
newAppName = QCoreApplication::self->d_func()->appName(); newAppName = QCoreApplication::self->d_func()->appName();
if (coreappdata()->application == newAppName) if (coreappdata()->application == newAppName)
return; return;
coreappdata()->application = newAppName; coreappdata()->application = newAppName;
coreappdata()->applicationNameCompat = newAppName;
#ifndef QT_NO_QOBJECT #ifndef QT_NO_QOBJECT
if (QCoreApplication::self) if (QCoreApplication::self)
emit QCoreApplication::self->applicationNameChanged(); emit QCoreApplication::self->applicationNameChanged();
@ -2374,7 +2376,7 @@ QString QCoreApplication::applicationName()
// Exported for QDesktopServices (Qt4 behavior compatibility) // Exported for QDesktopServices (Qt4 behavior compatibility)
Q_CORE_EXPORT QString qt_applicationName_noFallback() Q_CORE_EXPORT QString qt_applicationName_noFallback()
{ {
return coreappdata()->applicationNameCompat; return coreappdata()->applicationNameSet ? coreappdata()->application : QString();
} }
/*! /*!

View File

@ -112,6 +112,22 @@ void tst_QCoreApplication::qAppName()
// The application name should still be available after destruction; // The application name should still be available after destruction;
// global statics often rely on this. // global statics often rely on this.
QCOMPARE(QCoreApplication::applicationName(), QString::fromLatin1(appName)); QCOMPARE(QCoreApplication::applicationName(), QString::fromLatin1(appName));
// Setting the appname before creating the application should work (QTBUG-45283)
const QString wantedAppName("my app name");
{
int argc = 1;
char *argv[] = { const_cast<char*>(appName) };
QCoreApplication::setApplicationName(wantedAppName);
TestApplication app(argc, argv);
QCOMPARE(::qAppName(), QString::fromLatin1(appName));
QCOMPARE(QCoreApplication::applicationName(), wantedAppName);
}
QCOMPARE(QCoreApplication::applicationName(), wantedAppName);
// Restore to initial value
QCoreApplication::setApplicationName(QString());
QCOMPARE(QCoreApplication::applicationName(), QString());
} }
void tst_QCoreApplication::argc() void tst_QCoreApplication::argc()