How to implement the “Edit” menu with “Undo”, “Cut”, “Paste” and “Copy”?

后端 未结 4 508
野的像风
野的像风 2021-01-12 06:34

Greetings,

for one of my applications I\'m trying to implement an \"Edit\" menu. This menu usually has the standard-entries Undo, Cut, Copy

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 07:06

    user285740's solution didn't help me, since I'm using a browser control in my app (CEF or WebKit, doesn't matter).

    Why? For browsers, focusWidget() always seems to be NULL, since elements are not widgets. I tried postEvent() to other widgets - didn't work. Adding menu items with QAction::TextHeuristicRole + standard sequences like QKeySequence::Copy didn't do the job, too (I could connect them only to my slots, not to standard slots). Cefclient sample loads a xib file, but it's not an option for me, since Qt creates everything from scratch.

    Eventually, I found the solution! Create the same menu items from ObjectiveC++ code. They act like the ones created via QMenuBar, but (!) they can be connected to some real automatic actions like @selector(copy:)

    You can find an example here: nsMenuUtilsX::GetStandardEditMenuItem(), just do that from your ObjC++ code.

    But, this code doesn't work if you execute it before QApplication::exec(). Qt then "rewrites" the main menu programmatically... How to fix that? Well, maybe I'll add some hack like a QTimer. It doesn't do that if you don't try to add another items via QMenuBar. It's OK now! Qt-independent menu.

提交回复
热议问题