Fix empty window title regression, add application display name to window title

This increases consistency a lot: all windows and dialogs from a Qt
application will show the app display name in the caption, on Windows and X11.
This helps identifying which app a dialog belongs to, which is especially
useful when the dialog is very generic and shows up unexpectedly.

For compatibility reasons, the app name is added to the caption only
if setApplicationDisplayName() was called -- or if the caption would be
completely empty. The standard Qt4 case (setWindowTitle + no display name)
is unchanged.

Change-Id: Ib284c62c1f4c0bc923e5bc2d10247d95e9aa76c1
Reviewed-by: Samuel Rødal <samuel.rodal@digia.com>
This commit is contained in:
David Faure 2012-11-30 19:37:02 +01:00 committed by The Qt Project
parent 1a653225f6
commit 92bd9aecfb
7 changed files with 51 additions and 50 deletions

5
dist/changes-5.0.0 vendored
View File

@ -985,6 +985,11 @@ Qt for Windows CE
Please, notice that QMetaType::UnknownType has value 0, which previously was
reserved for QMetaType::Void.
- QWidget
* No need to set the application name in setWindowTitle() anymore, this is done
automatically, on Windows and Unix/X11, provided that the (possibly translated)
application display name is set with QGuiApplication::setApplicationDisplayName().
- QMessageBox

View File

@ -47,6 +47,7 @@ int main(int argc, char *argv[])
Q_INIT_RESOURCE(configdialog);
QApplication app(argc, argv);
app.setApplicationDisplayName("Qt Example");
ConfigDialog dialog;
return dialog.exec();
}

View File

@ -247,7 +247,12 @@ void QPlatformWindow::setParent(const QPlatformWindow *parent)
}
/*!
Reimplement to set the window title to \a title
Reimplement to set the window title to \a title.
The implementation might want to append the application display name to
the window title, like Windows and Linux do.
\sa QGuiApplication::applicationDisplayName()
*/
void QPlatformWindow::setWindowTitle(const QString &title) { Q_UNUSED(title); }

View File

@ -55,6 +55,7 @@
#include <QtGui/QWindow>
#include <QtGui/QRegion>
#include <private/qwindow_p.h>
#include <private/qguiapplication_p.h>
#include <qpa/qwindowsysteminterface.h>
#include <QtCore/QDebug>
@ -1188,8 +1189,21 @@ void QWindowsWindow::setWindowTitle(const QString &title)
{
if (QWindowsContext::verboseWindows)
qDebug() << __FUNCTION__ << this << window() <<title;
if (m_data.hwnd)
SetWindowText(m_data.hwnd, (const wchar_t*)title.utf16());
if (m_data.hwnd) {
QString fullTitle = title;
if (QGuiApplicationPrivate::displayName) {
// Append display name, if set.
if (!fullTitle.isEmpty())
fullTitle += QStringLiteral(" - ");
fullTitle += *QGuiApplicationPrivate::displayName;
} else if (fullTitle.isEmpty()) {
// Don't let the window title be completely empty, use the app name as fallback.
fullTitle = QCoreApplication::applicationName();
}
SetWindowText(m_data.hwnd, (const wchar_t*)fullTitle.utf16());
}
}
void QWindowsWindow::setWindowFlags(Qt::WindowFlags flags)

View File

@ -1116,7 +1116,18 @@ void QXcbWindow::setParent(const QPlatformWindow *parent)
void QXcbWindow::setWindowTitle(const QString &title)
{
QByteArray ba = title.toUtf8();
QString fullTitle = title;
if (QGuiApplicationPrivate::displayName) {
// Append display name, if set.
if (!fullTitle.isEmpty())
fullTitle += QString::fromUtf8(" \xe2\x80\x94 "); // unicode character U+2014, EM DASH
fullTitle += *QGuiApplicationPrivate::displayName;
} else if (fullTitle.isEmpty()) {
// Don't let the window title be completely empty, use the app name as fallback.
fullTitle = QCoreApplication::applicationName();
}
const QByteArray ba = fullTitle.toUtf8();
Q_XCB_CALL(xcb_change_property(xcb_connection(),
XCB_PROP_MODE_REPLACE,
m_window,

View File

@ -5474,18 +5474,6 @@ void QWidget::unsetLocale()
d->resolveLocale();
}
static QString constructWindowTitleFromFilePath(const QString &filePath)
{
QFileInfo fi(filePath);
QString windowTitle = fi.fileName() + QLatin1String("[*]");
#ifndef Q_WS_MAC
QString appName = QApplication::applicationName();
if (!appName.isEmpty())
windowTitle += QLatin1Char(' ') + QChar(0x2014) + QLatin1Char(' ') + appName;
#endif
return windowTitle;
}
/*!
\property QWidget::windowTitle
\brief the window title (caption)
@ -5502,6 +5490,11 @@ static QString constructWindowTitleFromFilePath(const QString &filePath)
windowModified property is false (the default), the placeholder
is simply removed.
On some desktop platforms (including Windows and Unix), the application name
(from QGuiApplication::applicationDisplayName) is added at the end of the
window title, if set. This is done by the QPA plugin, so it is shown to the
user, but isn't part of the \l windowTitle string.
\sa windowIcon, windowIconText, windowModified, windowFilePath
*/
QString QWidget::windowTitle() const
@ -5511,7 +5504,7 @@ QString QWidget::windowTitle() const
if (!d->extra->topextra->caption.isEmpty())
return d->extra->topextra->caption;
if (!d->extra->topextra->filePath.isEmpty())
return constructWindowTitleFromFilePath(d->extra->topextra->filePath);
return QFileInfo(d->extra->topextra->filePath).fileName() + QLatin1String("[*]");
}
return QString();
}
@ -5683,24 +5676,8 @@ QString QWidget::windowIconText() const
This property only makes sense for windows. It associates a file path with
a window. If you set the file path, but have not set the window title, Qt
sets the window title to contain a string created using the following
components.
On Mac OS X:
\list
\li The file name of the specified path, obtained using QFileInfo::fileName().
\endlist
On Windows and X11:
\list
\li The file name of the specified path, obtained using QFileInfo::fileName().
\li An optional \c{*} character, if the \l windowModified property is set.
\li The \c{0x2014} unicode character, padded either side by spaces.
\li The application name, obtained from the application's
\l{QCoreApplication::}{applicationName} property.
\endlist
sets the window title to the file name of the specified path, obtained using
QFileInfo::fileName().
If the window title is set at any point, then the window title takes precedence and
will be shown instead of the file path string.

View File

@ -230,28 +230,16 @@ void tst_QWidget_window::tst_windowFilePathAndwindowTitle_data()
QString validPath = QApplication::applicationFilePath();
QString fileNameOnly = QFileInfo(validPath).fileName() + QLatin1String("[*]");
QString fileAndSep = fileNameOnly + QLatin1String(" ") + QChar(0x2014) + QLatin1String(" ");
QString windowTitle = QLatin1String("Here is a Window Title");
QString defaultPlatString =
#if 0 // was ifdef Q_OS_MAC, but that code is disabled in qwidget.cpp and caption handling should move to QPA anyway
fileNameOnly;
#else
fileAndSep + "tst_qwidget_window"; // default app name in Qt5
#endif
QString defaultPlatString = fileNameOnly;
QTest::newRow("never Set Title nor AppName") << false << false << validPath << QString() << windowTitle << defaultPlatString << defaultPlatString;
QTest::newRow("set title after only, but no AppName") << false << true << validPath << QString() << windowTitle << defaultPlatString << windowTitle;
QTest::newRow("set title before only, not AppName") << true << false << validPath << QString() << windowTitle << windowTitle << windowTitle;
QTest::newRow("always set title, not appName") << true << true << validPath << QString() << windowTitle << windowTitle << windowTitle;
QString appName = QLatin1String("Killer App");
QString platString =
#if 0 // was ifdef Q_OS_MAC, but that code is disabled in qwidget.cpp and caption handling should move to QPA anyway
fileNameOnly;
#else
fileAndSep + appName;
#endif
QString appName = QLatin1String("Killer App"); // Qt4 used to make it part of windowTitle(), Qt5 doesn't anymore, the QPA plugin takes care of it.
QString platString = fileNameOnly;
QTest::newRow("never Set Title, yes AppName") << false << false << validPath << appName << windowTitle << platString << platString;
QTest::newRow("set title after only, yes AppName") << false << true << validPath << appName << windowTitle << platString << windowTitle;