From 92bd9aecfb69ebe525ba88dc78ce85e3ca7ed9c2 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 30 Nov 2012 19:37:02 +0100 Subject: [PATCH] Fix empty window title regression, add application display name to window title MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- dist/changes-5.0.0 | 5 +++ .../widgets/dialogs/configdialog/main.cpp | 1 + src/gui/kernel/qplatformwindow.cpp | 7 +++- .../platforms/windows/qwindowswindow.cpp | 18 ++++++++- src/plugins/platforms/xcb/qxcbwindow.cpp | 13 ++++++- src/widgets/kernel/qwidget.cpp | 39 ++++--------------- .../qwidget_window/tst_qwidget_window.cpp | 18 ++------- 7 files changed, 51 insertions(+), 50 deletions(-) diff --git a/dist/changes-5.0.0 b/dist/changes-5.0.0 index a969fc84d5e..84f0224bad7 100644 --- a/dist/changes-5.0.0 +++ b/dist/changes-5.0.0 @@ -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 diff --git a/examples/widgets/dialogs/configdialog/main.cpp b/examples/widgets/dialogs/configdialog/main.cpp index 20a2709a1ed..b23c64a56f6 100644 --- a/examples/widgets/dialogs/configdialog/main.cpp +++ b/examples/widgets/dialogs/configdialog/main.cpp @@ -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(); } diff --git a/src/gui/kernel/qplatformwindow.cpp b/src/gui/kernel/qplatformwindow.cpp index 88dccdb6cef..82547b04ec3 100644 --- a/src/gui/kernel/qplatformwindow.cpp +++ b/src/gui/kernel/qplatformwindow.cpp @@ -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); } diff --git a/src/plugins/platforms/windows/qwindowswindow.cpp b/src/plugins/platforms/windows/qwindowswindow.cpp index 2f2588bc4ac..40e0cf7dda6 100644 --- a/src/plugins/platforms/windows/qwindowswindow.cpp +++ b/src/plugins/platforms/windows/qwindowswindow.cpp @@ -55,6 +55,7 @@ #include #include #include +#include #include #include @@ -1188,8 +1189,21 @@ void QWindowsWindow::setWindowTitle(const QString &title) { if (QWindowsContext::verboseWindows) qDebug() << __FUNCTION__ << this << window() <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. diff --git a/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp b/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp index 230fcebfcbf..058831af2ed 100644 --- a/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp +++ b/tests/auto/widgets/kernel/qwidget_window/tst_qwidget_window.cpp @@ -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;