如何实现“编辑"带有“撤消"、“剪切"、“粘贴"的菜单和“复制"? [英] How to implement the "Edit" menu with "Undo", "Cut", "Paste" and "Copy"?
问题描述
您好,
对于我的一个应用程序,我正在尝试实现编辑"菜单.这个菜单通常有标准条目Undo、Cut、Copy 和Paste.
for one of my applications I'm trying to implement an "Edit" menu. This menu usually has the standard-entries Undo, Cut, Copy and Paste.
默认情况下没有这个菜单,用户似乎特别期待它在 Mac OS X 上.
This menu is not there by default, and users seem to expect it especially on Mac OS X.
有没有更简单的方法来实现这一点,而无需在每个小部件中手动执行?由于大多数小部件已经通过快捷方式实现了复制/粘贴/撤消机制,因此我想提供一些简单的菜单操作来调用它们.
Is there a an easier way of implementing this, without doing so in every widget manually? Since most widgets have the copy/paste/undo mechanism already implemented via shortcuts, I'd like to provide a few simple menu actions that call them as well.
我想这些动作应该首先调用任何具有焦点的小部件,然后它们应该将事件向上传递到对象链.
The actions should call whatever widget has the focus first, then they should pass the events upwards the object chain, I guess.
我在 Windows、Linux 和 Mac OS X 上使用 Qt 4.6.
I'm using Qt 4.6 on Windows, Linux and Mac OS X.
谢谢!
推荐答案
完成一半的必要功能很容易.只需在主窗口类中创建编辑菜单以及必要的 QAction(复制/粘贴/撤消/等)并将它们连接到插槽.在插槽中,模拟正确的按键按下和释放事件(例如 Ctrl+C 用于复制)并将它们发送到当前聚焦的小部件.在代码中,是这样的:
It's easy enough to accomplish half of the necessary functionality. Just create the Edit menu, along with the necessary QActions (copy/paste/undo/etc.) in your main window class and connect them to slots. In the slots, emulate the correct key press and release events (e.g. Ctrl+C for Copy) and send them to the currently focused widget. In code, something like this:
MainWindow::MainWindow(...)
{
...
connect( actionCopy, SIGNAL( triggered()), SLOT( copy()));
...
}
...
void MainWindow::copy()
{
QWidget* focused = QApplication::focusWidget();
if( focused != 0 )
{
QApplication::postEvent( focused,
new QKeyEvent( QEvent::KeyPress,
Qt::Key_C,
Qt::ControlModifier ));
QApplication::postEvent( focused,
new QKeyEvent( QEvent::KeyRelease,
Qt::Key_C,
Qt::ControlModifier ));
}
当然,这是一个相当大的黑客.您需要修改每个目标平台的代码,将键盘快捷键更改为正确的快捷键,并且可能会发生接收焦点的小部件使用 Ctrl+C 执行一些意外的安静操作.在我看来,这种方法最糟糕的缺点是您无法正确控制编辑"菜单项的启用状态.无法从通用小部件查询是否可以进行复制或粘贴操作.
Of course, this is quite a hack. You need to modify the code for each target platform, changing the keyboard shortcuts to the correct ones, and it might happen that a widget that receives focus does something quiet unexpected with Ctrl+C. The worst downside in this method, in my opinion, is that you cannot properly control the enabled state of the Edit menu items. It is not possible to query from a generic widget whether a copy or paste operation would be possible or not.
我无法找到解决此问题的真正解决方案 - 并且会惊讶地发现存在解决方案 - 因为复制/粘贴功能通常隐藏在类的代码中,并且不会通过任何标准信号集暴露/插槽.在今晚对功能进行实验之后,我决定忘记我的应用程序中的编辑"菜单,并希望用户知道键盘快捷键或使用上下文相关菜单.
I am unable to find a real solution to this problem - and would be surprised to find out that one exists - as the copy/paste functionality is generally hidden inside the class' code and not exposed through any standard set of signals/slots. After tonight's experiments with the functionality, I have decided to just forget having an Edit menu from my application and expect the users to know the keyboard shortcuts, or use the context sensitive menus.
这篇关于如何实现“编辑"带有“撤消"、“剪切"、“粘贴"的菜单和“复制"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!