QML Windows MenuBar: Can't cut or copy because MenuItems steal focus
-
Using qt 6.3.0 and 6.4.1 on Windows, with either Windows style or Fusion style. I have a standard QML MenuBar (qt-native, not qt.labs platform) with an Edit menu; the Edit Menu has Cut, Copy, and Paste MenuItems that are supposed to act on the currently focused text control (a TextExit or TextArea). I have the MenuBar's focusPolicy set to Qt.NoFocus, the Menus' focus set to false, and the MenuItems' focusPolicy set to Qt.NoFocus.
I have confirmed that the desired text control's activeFocus is true, and I have text selected. When I click in the Edit menu, activeFocus does not change, but as soon as I move the mouse pointer over any enabled MenuItem, activeFocus changes to that MenuItem (or a QQuickRootItem or a QQuickListView, which I'm guessing are Menu or MenuItem components) and my text selection is canceled. If I click outside the menu or hit Esc, activeFocus returns to the previously selected text control, but my text selection is gone. This makes it impossible to copy text as the selection is deleted as soon as the Cut or Copy MenuItems are chosen.
Isn't "MenuItem.focusPolicy: Qt.NoFocus" supposed to prevent the focus from shifting to the MenuItems? Am I missing something simple here?
(This seems similar to https://forum.qt.io/topic/133679/pasting-from-main-menu. I hope there is a way to fix this without catching the QApplication::focusChanged signal and then doing my own handling)
-
Using qt 6.3.0 and 6.4.1 on Windows, with either Windows style or Fusion style. I have a standard QML MenuBar (qt-native, not qt.labs platform) with an Edit menu; the Edit Menu has Cut, Copy, and Paste MenuItems that are supposed to act on the currently focused text control (a TextExit or TextArea). I have the MenuBar's focusPolicy set to Qt.NoFocus, the Menus' focus set to false, and the MenuItems' focusPolicy set to Qt.NoFocus.
I have confirmed that the desired text control's activeFocus is true, and I have text selected. When I click in the Edit menu, activeFocus does not change, but as soon as I move the mouse pointer over any enabled MenuItem, activeFocus changes to that MenuItem (or a QQuickRootItem or a QQuickListView, which I'm guessing are Menu or MenuItem components) and my text selection is canceled. If I click outside the menu or hit Esc, activeFocus returns to the previously selected text control, but my text selection is gone. This makes it impossible to copy text as the selection is deleted as soon as the Cut or Copy MenuItems are chosen.
Isn't "MenuItem.focusPolicy: Qt.NoFocus" supposed to prevent the focus from shifting to the MenuItems? Am I missing something simple here?
(This seems similar to https://forum.qt.io/topic/133679/pasting-from-main-menu. I hope there is a way to fix this without catching the QApplication::focusChanged signal and then doing my own handling)
I was overthinking it: if I don't bother setting MenuBar and MenuItem focusPolicy to NoFocus, and Menu focus to false, my text item's selection is unchanged as I traverse menus.
All I needed to do is set "editItem" to the window's activeFocusItem, as long as it has the window in its ancestor list (menus and menu items are not parented by the window), or is null, and then reference editItem in my cut/copy/paste actions instead of activeFocusItem.
property Item editItem: null; ... onActiveFocusItemChanged: setEditItem(); function setEditItem() { // we need to set EditItem to the currently focused Item as long as it's a child // of a Window, which menu items are not, thus preventing focused menu items // from "stealing" the edit focus. // see if we're in the menuBar (not a window's descendant) var inMenu = true; // assume it until shown otherwise var thisItem = activeFocusItem; while (thisItem) { if (thisItem.objectName === "ApplicationWindow") { inMenu = false; break; } else thisItem = thisItem.parent; } // set editItem to any non-menu item, or null if focusedItem is null if (!inMenu || !activeFocusItem) editItem = activeFocusItem; }