PDA

View Full Version : How to disable CTRL+V on a QLineWidget



sanket.mehta
3rd December 2010, 09:35
Hi,

I have one QLineWidget with echo mode as password.
Now I want to disable CTRL+V on that QlineWidget as I do not want that anybody can paste on that widget using CTRL+V shortcut.

I have set context menu::No Context Menu which will disable the right click options but I do not know how to disable CTRL+V only on this QLineWidget.

Can anybody can help me?

Thanks in advance.
Sanket

wysota
3rd December 2010, 09:44
There are a couple of ways to do it. One is to subclass QLineEdit and redeclare (that's important!) and reimplement the paste() slot from it to do nothing.

high_flyer
3rd December 2010, 09:45
In addition to what wysota suggested:
Override the general event handler, or install and eventFileter on the widget, and you can do the following:
a. I don't know if QLineEdit is using QShortcutEvent events - you can tests that, in your event handler or look at QLineEdit code.
If it does use the QShortcutEvent - then you just intercept the one you are interested in.
b. Provided a. is not the case, you can just intercept the normal keypress events an evaluate the current pressed keys.

But wysota's way is cleaner I think.

wysota
3rd December 2010, 10:15
I think that going through key events will not be sufficient, at least on some platforms. While on Windows this might be sufficient, on Unix you can usually paste something from clipboard using the middle mouse button which does not send any key events. That's my guess at least.

SixDegrees
3rd December 2010, 10:31
Something else to keep in mind: a lot of people, like myself, hate it when others decide what's best for them when it comes to using their own computer. Consider carefully whether the benfits of disabling Ctrl-V outweight the costs in incovnenience.