vcl/inc/qt5/QtMenu.hxx |    2 +-
 vcl/qt5/QtMenu.cxx     |   38 ++++++++++++++++++++------------------
 2 files changed, 21 insertions(+), 19 deletions(-)

New commits:
commit 45d7a65f3d3dadcd71807a290fc11ff546aa5ba7
Author:     Jan-Marek Glogowski <glo...@fbihome.de>
AuthorDate: Tue Apr 5 16:49:38 2022 +0200
Commit:     Jan-Marek Glogowski <glo...@fbihome.de>
CommitDate: Wed Apr 6 15:16:47 2022 +0200

    tdf#145954 Qt unshare QMenubar usage
    
    The Qt code was sharing the menu bar from the top level frame, but
    LO expects independent menu bars per SetFrame calls. So instead of
    showing the new bar and then hiding the old one, this was always
    show and hiding the same menu bar, resulting in a hidden menu bar.
    
    As a result of unsharing, LO now must check that its menu bar
    pointer is still valid for usage. The QMainWindows takes ownership
    when a QMenuBar is assigned and destroy old ones.
    
    Change-Id: I2c6b12199a1e17a5d9f88686a4b27b1413beda47
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132581
    Tested-by: Jenkins
    Reviewed-by: Jan-Marek Glogowski <glo...@fbihome.de>
    (cherry picked from commit 9c4ef8ce3183e27ca174475cf4a8d15cc0368f60)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/132604
    Reviewed-by: Michael Weghorn <m.wegh...@posteo.de>

diff --git a/vcl/inc/qt5/QtMenu.hxx b/vcl/inc/qt5/QtMenu.hxx
index 55275ae6e099..f39be7e8d506 100644
--- a/vcl/inc/qt5/QtMenu.hxx
+++ b/vcl/inc/qt5/QtMenu.hxx
@@ -50,7 +50,6 @@ private:
     // pointer to QMenu owned by the corresponding QtMenuItem or self (-> 
mpOwnedQMenu)
     QMenu* mpQMenu;
     QPushButton* mpCloseButton;
-    QMetaObject::Connection maCloseButtonConnection;
 
     void DoFullMenuUpdate(Menu* pMenuBar);
     static void NativeItemText(OUString& rItemText);
@@ -60,6 +59,7 @@ private:
     void ReinitializeActionGroup(unsigned nPos);
     void ResetAllActionGroups();
     void UpdateActionGroupItem(const QtMenuItem* pSalMenuItem);
+    bool validateQMenuBar();
 
 public:
     QtMenu(bool bMenuBar);
diff --git a/vcl/qt5/QtMenu.cxx b/vcl/qt5/QtMenu.cxx
index c6224b137665..44873ce3384d 100644
--- a/vcl/qt5/QtMenu.cxx
+++ b/vcl/qt5/QtMenu.cxx
@@ -59,7 +59,7 @@ void QtMenu::InsertMenuItem(QtMenuItem* pSalMenuItem, 
unsigned nPos)
     if (mbMenuBar)
     {
         // top-level menu
-        if (mpQMenuBar)
+        if (validateQMenuBar())
         {
             QMenu* pQMenu = new QMenu(toQString(aText), nullptr);
             pSalMenuItem->mpMenu.reset(pQMenu);
@@ -428,20 +428,10 @@ void QtMenu::SetFrame(const SalFrame* pFrame)
     if (!pMainWindow)
         return;
 
-    mpQMenuBar = pMainWindow->menuBar();
-    if (mpQMenuBar)
-    {
-        mpQMenuBar->clear();
-        QPushButton* pButton
-            = 
static_cast<QPushButton*>(mpQMenuBar->cornerWidget(Qt::TopRightCorner));
-        if (pButton && ((mpCloseButton != pButton) || 
!maCloseButtonConnection))
-        {
-            maCloseButtonConnection
-                = connect(pButton, &QPushButton::clicked, this, 
&QtMenu::slotCloseDocument);
-            mpCloseButton = pButton;
-        }
-    }
+    mpQMenuBar = new QMenuBar();
+    pMainWindow->setMenuBar(mpQMenuBar);
 
+    mpCloseButton = nullptr;
     mpQMenu = nullptr;
 
     DoFullMenuUpdate(mpVCLMenu);
@@ -565,9 +555,22 @@ QtMenu* QtMenu::GetTopLevel()
     return pMenu;
 }
 
+bool QtMenu::validateQMenuBar()
+{
+    if (!mpQMenuBar)
+        return false;
+    assert(mpFrame);
+    QtMainWindow* pMainWindow = mpFrame->GetTopLevelWindow();
+    assert(pMainWindow);
+    const bool bValid = mpQMenuBar == pMainWindow->menuBar();
+    if (!bValid)
+        mpQMenuBar = nullptr;
+    return bValid;
+}
+
 void QtMenu::ShowMenuBar(bool bVisible)
 {
-    if (mpQMenuBar)
+    if (validateQMenuBar())
         mpQMenuBar->setVisible(bVisible);
 }
 
@@ -643,7 +646,7 @@ void QtMenu::slotCloseDocument()
 
 void QtMenu::ShowCloseButton(bool bShow)
 {
-    if (!mpQMenuBar)
+    if (!validateQMenuBar())
         return;
 
     QPushButton* pButton = 
static_cast<QPushButton*>(mpQMenuBar->cornerWidget(Qt::TopRightCorner));
@@ -661,8 +664,7 @@ void QtMenu::ShowCloseButton(bool bShow)
         pButton->setFocusPolicy(Qt::NoFocus);
         pButton->setToolTip(toQString(VclResId(SV_HELPTEXT_CLOSEDOCUMENT)));
         mpQMenuBar->setCornerWidget(pButton, Qt::TopRightCorner);
-        maCloseButtonConnection
-            = connect(pButton, &QPushButton::clicked, this, 
&QtMenu::slotCloseDocument);
+        connect(pButton, &QPushButton::clicked, this, 
&QtMenu::slotCloseDocument);
         mpCloseButton = pButton;
     }
 

Reply via email to