[Qt-interest] Need some help about QLineEdit
Girish Ramakrishnan
girish at forwardbias.in
Mon Oct 17 06:08:49 CEST 2011
Hi,
On Sun, Oct 16, 2011 at 6:24 PM, Zeljko <zeljko at holobit.net> wrote:
> Andre Somers wrote:
>
>> Op 16-10-2011 13:05, Zeljko schreef:
>>> Andre Somers wrote:
>>>
>>>> Op 16-10-2011 11:06, Zeljko schreef:
>>>>> Hi,
>>>>> As I can see there's no way to catch copy(),paste() and cut() ops with any
>>>>> event/signal ?
>>>>> I need to intercept those to know when user copied , pasted and
>>>>> cutted data from QLineEdit and QTextEdit and eventually forbid operation.
>>>>> I know that QValidator helps in case of paste() , but what about copy() and
>>>>> cut() ?
>>>>> Is there any solution (or even planned in 5.0) for such problem (of course
>>>>> there's + selection copy under X11 ...) ?
>>>>>
>>>> You should try an event filter, and filter out the key events.
>>> I can do that for user input events eg. pressed Ctrl +C , but what about
>>> context menu actions ? When user triggers copy() via context menu (default
>>> one) there's no events in event filter that shortcut triggered (Ctrl + C) or
>>> anything similar.
>>>
>> You can show your own context menu, right? You can listen to the
>> contextMenuEvent, or use the QWidget::contextMenuPolicy to change if and
>> how a context menu is shown.
>
> Of course I can do that, but asked because of another possibility ... seem
> there's nothing except intercept keys and build my own context menu.
>
cut(), copy(), paste() are all slots. So, you can just subclass
QLineEdit and create your own slots cut(), copy(), paste(). Thanks to
the way signal/slots work, your implementation will be called when the
actions are triggered. Note that cut(), copy() and paste() are not
virtual slots. As a result, the above approach is a bit dangerous i.e
if QLineEdit calls cut() in c++ then it will call QLineEdit's cut()
and not you reimplemented cut(). AFAIK, QLineEdit does not call these
methods directly, so it should work in your case.
Another approach: Apart from filtering keyboard keys, you should also
reimplement contextMenuEvent and call createStandardContextMenu,
install an event filter on the menu and then pop it up.
BTW, it looks like you just want a readOnly lineedit? Why would
someone disable cut/paste/copy in a editable lineedit? (I think
password mode edits have copy/paste disabled anyway)
Girish
More information about the Qt-interest-old
mailing list