如何实现“编辑"功能.带有“撤消",“剪切",“粘贴"菜单的菜单.和“复制"? [英] How to implement the "Edit" menu with "Undo", "Cut", "Paste" and "Copy"?
问题描述
问候,
对于我的一个应用程序,我正在尝试实现编辑"菜单.此菜单通常具有标准条目撤消,剪切,复制和粘贴.
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 ));
}
当然,这确实是一个hack.您需要修改每个目标平台的代码,将键盘快捷方式更改为正确的快捷方式,并且可能会发生这样的情况:获得焦点的小部件在执行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屋!