Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem updating QDockWidget's title with translations
Forum Updated to NodeBB v4.3 + New Features

Problem updating QDockWidget's title with translations

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 746 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qt-public-name
    wrote on last edited by
    #1

    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.

    Capture d’écran 2023-05-11 162340.png
    QDockWidget when floating, title is correctly translated

    Capture d’écran 2023-05-11 162409.png
    QDockWidget when attached to the QMainWindow (not floating), title is not correctly translated

    Is 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

    SGaistS 1 Reply Last reply
    0
    • Q qt-public-name

      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.

      Capture d’écran 2023-05-11 162340.png
      QDockWidget when floating, title is correctly translated

      Capture d’écran 2023-05-11 162409.png
      QDockWidget when attached to the QMainWindow (not floating), title is not correctly translated

      Is 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

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Can you show how you manage your dock widgets and their title ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Can you show how you manage your dock widgets and their title ?

        Q Offline
        Q Offline
        qt-public-name
        wrote on last edited by
        #3

        @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.

        Axel SpoerlA 1 Reply Last reply
        0
        • Q qt-public-name

          @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.

          Axel SpoerlA Offline
          Axel SpoerlA Offline
          Axel Spoerl
          Moderators
          wrote on last edited by
          #4

          @qt-public-name
          Hi,

          you have found a bug. Sorry about that.

          setWindowTitle()is a function of QWidget, which QDockWidgetis 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's topLevelChangedsignal, that propagates the member variable to the title when docked. That forces the titles back into sync.

          Cheers
          Axel

          untested 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);
          }
          

          Software Engineer
          The Qt Company, Oslo

          Q 1 Reply Last reply
          2
          • Axel SpoerlA Axel Spoerl

            @qt-public-name
            Hi,

            you have found a bug. Sorry about that.

            setWindowTitle()is a function of QWidget, which QDockWidgetis 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's topLevelChangedsignal, that propagates the member variable to the title when docked. That forces the titles back into sync.

            Cheers
            Axel

            untested 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);
            }
            
            Q Offline
            Q Offline
            qt-public-name
            wrote on last edited by
            #5

            @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

            Axel SpoerlA 1 Reply Last reply
            1
            • Q qt-public-name

              @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

              Axel SpoerlA Offline
              Axel SpoerlA Offline
              Axel Spoerl
              Moderators
              wrote on last edited by
              #6

              @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 QStringmember. The downside is that my_dock_widgetis not self-contained. It depends on an implementation in another class (MainWindow), to remain consistent. Any user recycling my_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.

              Software Engineer
              The Qt Company, Oslo

              Axel SpoerlA 1 Reply Last reply
              0
              • Axel SpoerlA Axel Spoerl

                @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 QStringmember. The downside is that my_dock_widgetis not self-contained. It depends on an implementation in another class (MainWindow), to remain consistent. Any user recycling my_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.

                Axel SpoerlA Offline
                Axel SpoerlA Offline
                Axel Spoerl
                Moderators
                wrote on last edited by
                #7

                @qt-public-name
                Once more: Can you please point me to the bug report? I can't find it. Thanks!

                Software Engineer
                The Qt Company, Oslo

                Q 1 Reply Last reply
                0
                • Axel SpoerlA Axel Spoerl

                  @qt-public-name
                  Once more: Can you please point me to the bug report? I can't find it. Thanks!

                  Q Offline
                  Q Offline
                  qt-public-name
                  wrote on last edited by
                  #8

                  @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.

                  Axel SpoerlA 1 Reply Last reply
                  0
                  • Q qt-public-name

                    @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.

                    Axel SpoerlA Offline
                    Axel SpoerlA Offline
                    Axel Spoerl
                    Moderators
                    wrote on last edited by
                    #9

                    @qt-public-name
                    Thanks, I got it.

                    Software Engineer
                    The Qt Company, Oslo

                    Axel SpoerlA 1 Reply Last reply
                    0
                    • Axel SpoerlA Axel Spoerl

                      @qt-public-name
                      Thanks, I got it.

                      Axel SpoerlA Offline
                      Axel SpoerlA Offline
                      Axel Spoerl
                      Moderators
                      wrote on last edited by
                      #10

                      @Axel-Spoerl
                      Fix is in the oven.
                      (I'm not always that fast, so don't tell anybody ;-)

                      Software Engineer
                      The Qt Company, Oslo

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved