Problem updating QDockWidget's title with translations
-
Hello,
I have multiple QDockWidgets in my app which can be translated dynamically. My problem concerns the dynamic translation of the title of the QDockWidget. I tried using setWindowTitle() like adviced here and it works when the QDockWidget is floating. However, when the QDockWidget is not floating, the title is the same as the one used when creating the QDockWidget.
QDockWidget when floating, title is correctly translated
QDockWidget when attached to the QMainWindow (not floating), title is not correctly translatedIs there a way to show a correctly translated title without having to customize the title bar with a custom widget ?
Thanks.
OS: Windows 11
Qt: 6.4.3 -
Hi and welcome to devnet,
Can you show how you manage your dock widgets and their title ?
-
@SGaist Hello!
I created a sample project and tested setting the dock widget title (using setWindowTitle()) from the MainWindow, it actually works as expected.
What I am doing in my project is overriding the QDockWidget class into a custom class, intercepting the LanguageChange event, and setting the dock widget title there. This produces the effect I described above. See the following code below:class MyDockWidget : public QDockWidget { Q_OBJECT public: explicit MyDockWidget( QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); protected: void changeEvent(QEvent* event) override; }; MyDockWidget::MyDockWidget( QWidget *parent, Qt::WindowFlags flags) : QDockWidget(tr("Title"), parent) { } void MyDockWidget::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { setWindowTitle(tr("Title")); } }
I am wondering if setWindowTitle() can be used inside the custom QDockWidget class instead of the MainWindow.
-
@qt-public-name
Hi,you have found a bug. Sorry about that.
setWindowTitle()
is a function ofQWidget
, whichQDockWidget
is inheriting from. The function sets the title on- the window handle, when the dock widget is floating
- on the title bar, when it's docked
When a dock widget gets unplugged from the main window, it reads and propagates the window title. The case that the title changes while it's undocked is not covered => The values get out of sync.
Please file a bug at https://bugreports.qt.io
You can assign it to me if you want. I'd give it a P2 because runtime title changes are rather a corner case.You can implement an immediate workaround as follows:
Add a private member to store the translated title whenever the language changes.
Connect a private slot to the dock widget'stopLevelChanged
signal, that propagates the member variable to the title when docked. That forces the titles back into sync.Cheers
Axeluntested code:
class MyDockWidget : public QDockWidget { Q_OBJECT public: explicit MyDockWidget( QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); protected: void changeEvent(QEvent* event) override; private: QString m_title; private slots: void propagateTitle(bool topLevel); }; MyDockWidget::MyDockWidget( QWidget *parent, Qt::WindowFlags flags) : QDockWidget(tr("Title"), parent) { m_title = windowTitle(); connect(this, &MyDockWidget::propagateTitle, this, &QDockWidget::topLevelChanged); } void MyDockWidget::changeEvent(QEvent* event) { if (event->type() == QEvent::LanguageChange) { setWindowTitle(tr("Title")); m_title = windowTitle(); } } void MyDockWidget::propagateTitle(bool topLevel) { if (!topLevel) setWindowTitle(m_title); }
-
@Axel-Spoerl Hello!
Thanks for the detailed answer. I just filed the bug.
Another workaround that I did was simply assign the qdockwdiget's title from the main window like the following:void MainWindow::changeEvent(QEvent* event) { switch (event->type()) { case QEvent::LanguageChange: m_ui->retranslateUi(this); // Dynamically update windows' title in MainWindow. // The code does not work as expected within the MyDockWidget class. for (MyDockWidget* my_dock_widget : findChildren<MyDockWidget*>()) { my_dock_widget->setWindowTitle(my_dock_widget->title()); } break; default: QWidget::changeEvent(event); break; } }
Cheers
-
@qt-public-name said in Problem updating QDockWidget's title with translations:
Another workaround that I did was simply assign the qdockwdiget's title from the main window like the following:
That would probably work as well, sure. The beauty of it is that it spares an additional
QString
member. The downside is thatmy_dock_widget
is not self-contained. It depends on an implementation in another class (MainWindow
), to remain consistent. Any user recyclingmy_dock_widget
will chase you because of my bug. Not that it bothers me... ;-)@qt-public-name said in Problem updating QDockWidget's title with translations:
I just filed the bug.
Could you point me to the bug report, please? I somehow fail to find it.
Thanks in advance. -
@qt-public-name
Once more: Can you please point me to the bug report? I can't find it. Thanks! -
@Axel-Spoerl
Sorry it seems I do not find how to modify the assignee field on the page (it is currently assigned to Qt Quick and Widgets Team). Here is the link of the bug report page. -
@qt-public-name
Thanks, I got it. -
@Axel-Spoerl
Fix is in the oven.
(I'm not always that fast, so don't tell anybody ;-)