Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Windows MenuBar: Can't cut or copy because MenuItems steal focus

QML Windows MenuBar: Can't cut or copy because MenuItems steal focus

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 329 Views
  • 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.
  • A Offline
    A Offline
    Adam Wilt
    wrote on 3 Jan 2023, 06:15 last edited by Adam Wilt 1 Mar 2023, 19:39
    #1

    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)

    A 1 Reply Last reply 4 Jan 2023, 03:15
    0
    • A Adam Wilt
      3 Jan 2023, 06:15

      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)

      A Offline
      A Offline
      Adam Wilt
      wrote on 4 Jan 2023, 03:15 last edited by Adam Wilt 1 Apr 2023, 22:51
      #2

      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;
          }
      
      1 Reply Last reply
      0

      1/2

      3 Jan 2023, 06:15

      • Login

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