开发者

Qt: How to display Ctrl+W instead of Ctrl+F4 in the default system menu of a subwindow?

开发者 https://www.devze.com 2023-04-11 16:41 出处:网络
some quick help is needed, as I am probably missing the trick that would help with this situation in Qt (I use the latest Qt 4).

some quick help is needed, as I am probably missing the trick that would help with this situation in Qt (I use the latest Qt 4).

Within my QMdiArea, I create a few subwindows. All the subwindows have the default system menu attached (minimize, maximize, stay on top, close, ... - that's seen when right-clicking on the subwindow icon).

To be consistent with what I have defined in the main menu of my application (ctrl+w visible for closing windows), I cannot accept that the displayed shortcut related to Close is Ctrl+F4 in the subwindows' system menu. It has to be Ctrl+w. I have开发者_Python百科 tried different things, including setting the shortcut global for the application (with setShortcutContext)... but no luck.

Close has by default two shortcuts: Ctrl+w and Ctrl+F4. I want both to keep working, but it's Ctrl+w which should always be displayed.

For now, the only solution I see is to replace the system menu (a QMenu)... but that seem to be a lot of code for such a simple task!


You can try setting it this way:

ui->mdiArea->subWindowList().at(index)->systemMenu()->actions().last()->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_W));

Please replace "index" with suitable index. It worked for me. (UPDATE: this does not work, CTRL+W is already an assigned shortcut for close action)

UPDATE:

Close action has two assigned shortcuts, and the first one is displayed at menu. If you swap the order, you'll get CTRL+W displayed.

QList<QKeySequence> closeShorcuts = ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->shortcuts();
closeShorcuts.swap(0, 1);
ui->mdiArea->subWindowList().at(0)->systemMenu()->actions().last()->setShortcuts(closeShorcuts);

Please note that there is no platform check in this source code; if you run this on MAC you'll get the opposite result, and in X11 it will go out of list bounds (because there is only one shortcut: CTRL+W).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号