PDA

View Full Version : Is there a way to force QLineEdit to start accepting input with Hebrew as default



dave
11th July 2010, 11:23
I have a program which is used on windows systems, running Hebrew & English as the default languages. My users are not tech savvy at all (bunch of old granpas :)). Now on each run of the program it first ask the user for a name and password - and the name must be in Hebrew (requirements are not mine). I haven't found a way to make sure the program start accepting input with Hebrew as the default so on each start they have to switch to language to Hebrew (using the OS pre-assigned key combination) and then input their user names. Since they start up the program quite of a few times each day this is bugging them.

I have tried using setLocale() to force Hebrew as the locale for the program and it works for all program defined strings, but not anywhere outside input is concerned.

So to my question, is it even possible to force QLineEdit to start accepting input with Hebrew as default?


Thanks for any answer.
Dave

tbscope
11th July 2010, 11:35
If I understand you correctly, you only want to have the Hebrew locale for the line edit?
If so, you can set a locale per widget:
http://doc.qt.nokia.com/4.6/qwidget.html#locale-prop

wysota
11th July 2010, 11:46
I think the input method (keyboard layout) is the problem here, not the locale so the proper way would be to programatically switch the Windows keyboard layout to hebrew. In other words it's not the problem with data display (something locale is related to) but data input.

dave
11th July 2010, 11:46
Thanks for answering.


If I understand you correctly, you only want to have the Hebrew locale for the line edit?
If so, you can set a locale per widget:
http://doc.qt.nokia.com/4.6/qwidget.html#locale-prop

That's what I did. Unfortunately it doesn't affect the input language.

Lykurg
11th July 2010, 11:50
שלום,

I am afraid but this is OS specific since you want to change the keyboard layout. But I am sure you can change it via the system API every time the line edit gets the focus. (Or, ask the user for the shortcuts he normal uses to change the kb layout and trigger it programmatic.)

Or (not the best way) install a event handler and check for keyboard inputs and transform e.g. a "D" to "ד".

Lykurg

dave
11th July 2010, 12:41
I think the input method (keyboard layout) is the problem here, not the locale so the proper way would be to programatically switch the Windows keyboard layout to hebrew. In other words it's not the problem with data display (something locale is related to) but data input.

That's also the conclusion Lykurg reached. I didn't think of it this way but you're both right.


שלום,

I am afraid but this is OS specific since you want to change the keyboard layout. But I am sure you can change it via the system API every time the line edit gets the focus. (Or, ask the user for the shortcuts he normal uses to change the kb layout and trigger it programmatic.)

Or (not the best way) install a event handler and check for keyboard inputs and transform e.g. a "D" to "ד".

Lykurg

שלום וברכה :)

I think that to trigger the key combination programmatically is the best choice. But I have no idea how to go about it. How would I trigger, for example, a shift and an alt keypress together as an input to the QLineEdit?

Thanks again

Lykurg
11th July 2010, 12:56
since I am a fan of subclassing, I made a small example with subclassing QLineEdit, but the same could be achieved with an event filter.

class HebrewInput : public QLineEdit
{
public:
HebrewInput(QWidget *parent = 0) : QLineEdit(parent)
{
setLocale(QLocale::Hebrew);
}

protected:
void keyPressEvent ( QKeyEvent * event )
{
event->accept();
// determine which key is pressed and what hebrew letter it should be. ("switch" over event->key())
QKeyEvent e(QEvent::KeyPress,
Qt::Key_unknown,
Qt::NoModifier,
QString::fromUtf8("ש"));
QLineEdit::keyPressEvent(&e);
}
};

dave
11th July 2010, 13:04
@Lykurg

Thanks again. That'll work perfectly.

wysota
11th July 2010, 13:19
Isn't it easier to switch the keyboard layout on focusInEvent and switch it back on focusOutEvent?

dave
11th July 2010, 20:16
Sorry for being obtuse, but how do I switch the keyboard layout?

P.S in QApplication I found two function that might help: keyboardInputDirection() and keyboardInputLocale() but unfortunately they have no set equivalent.

Lykurg
11th July 2010, 21:36
I never have done that myself and on linux it might be complicated because it depends on the graphical environment (KDE, Gnome, ...).

For windows you can have a look at http://msdn.microsoft.com/en-us/library/ff468859%28v=VS.85%29.aspx: ActivateKeyboardLayout, GetKeyboardLayout and GetKeyboardLayoutList.

wysota
11th July 2010, 21:44
On KDE there is probably some dbus call that will do the trick.