PDA

View Full Version : Detecting where "Paste" should paste to



codeslicer
21st January 2009, 00:42
Hi, I have a question. Say I have an edit menu in my application, with different commands such as copy, paste, etc. When an object without text input has focus, I want the menu items in the edit menu disabled. And since I have multiple objects that accept input, I want the menu items to act on the active object, only if it accepts input.

Any ideas on how to do this would be greatly appreciated. I see QCreator does it but I couldn't really find anything helpful in the huge code.

Thanks in advance,
codeslicer :)

aamer4yu
21st January 2009, 08:56
You might use QApplication::focusChanged to get the widget in focus and typecast the widget to desired text input classes like line edit, textedit etc, and update your menu item accordingly.

You might also check if the widget has "text" property and perform actions using it.

mchara
26th January 2009, 10:40
use QApplication::focusChanged() signal to detect when focus changes

To check if actions should be enabled or not, you may check FocusPolicy - widgets without text input have usually set noFocus or tabFocus while editable items have often strong focus.

You can also use qobject_cast<>() to determine if widget is a derrived from particullar class i.e. QAbstractSpinbox, QLineEdit, QTextEdit, QComboBox an so on.

Next thing to do would be propagating copy, paste actions to focused widgets. I would suggest to create proper QShortcutEvent in each action and post it to focused widget with qApp->postEvent().

Take also into consideration that using keyboard sequences like ctrl+C should work on focused widget(if it accepts such keysequence) without any additional implementations, because qt tries to deliver shortcutEvents to focused widget first and if it fails(widget doesn't accept a shortcut) it's delivered to widgets that are higher in hierarchy (so finally it reaches main window and it's menubar).