I have a QLineEdit for Date in mm/dd/yyyy format. I am getting input using the keyboard and not using QDateEdit because of the requirement. And when the lineEdit comes to view, it has to show to the user the current date. I need the following for the lineEdit.
I need the two slashes always to be displayed and the cursor has to skip while entering or deleting.
I should not allow the user to enter an invalid date i.e while entering itself the lineEdit should not get invalid numbers.
I have to set the current date as the default text when the lineEdit comes to view.
For the first point, I tried using setInputMask("99/99/9999") but with this I can't set the current date using setText(). And how to use QRegExp to not to allow lineEdit get an invalid number while employing setInputMask()?
QDateEdit will serve your purpose.
use setDisplayFormat("dd/MM/yyyy").
QDateEdit wont allow invalid dates
You can use QDateEdit::setDate() obtained from
QDateTime::currentDateTime()
For setting text into QLineEdit with setInputMask("99/99/9999") you should format text depending on your mask:
lineEdit.setText("{:02d}/{:02d}/{:04d}".format(m, d, y))
Alternatively, you can temporary disable InputMask, format your date without /, set it and re-enable InputMask. But make sure that number of symbols in every part is correct.
lineEdit.setInputMask("")
lineEdit.setText(date_str.replace("/", ""))
lineEdit.setInputMask("99/99/9999")
Related
I want to input a number in QLienEdit and the range should during 0 and the max number we have got.
Use QLineEdit::setValidator
Sets the validator for values of line edit to v.
The line edit's returnPressed() and editingFinished() signals will only be emitted if v validates the line edit's content as Acceptable. The user may change the content to any Intermediate value during editing, but will be prevented from editing the text to a value that v validates as Invalid.
This allows you to constrain the text that shall finally be entered when editing is done, while leaving users with enough freedom to edit the text from one valid state to another.
If v == 0, setValidator() removes the current input validator. The initial setting is to have no input validator (i.e. any input is accepted up to maxLength()).
The following examples show you how to use this function.
https://doc.qt.io/qt-5/qtwidgets-widgets-lineedits-example.html
Qt QLineEdit Input Validation
I don't understand. I setup char format, block format, root frame format, and page size for all of the text in QTextEdit control. And then if I manually delete all the text, and start to type new one, or if I select all text and paste new one from buffer, then voilà! - all the formatting loses.
Is it possible to set some default format for QTextEdit (char's, block's, page, etc.)?
I've solved it the next way.
Handled QTextEdit::currentCharFormatChanged signal(as vahancho promted), and call QTextEdit::setTextCursor with needed formatting cursor. It solves the problem with char and block format.
For the pageSize and rootFrame's format, I've handled QTextEdit::document::documentLayout's update signal and if rootFrame format or pageSize of the document was changed, then resetup the needed sizes again.
I have made an editor in which i have to enter a Hex or decimal value in each field. Here the field i am using is QLineEdit.
Now requirement is that each Qlineedit box accept only one value without spaces. Then i can read this text value & convert it directly from string to decimal.
Is it possible to make QlineEdit to accept only one value without spaces ? Boxes in below figure are QLineEdit.
I do not want to use combo box here.
If the answer was just decimal, I'd suggest you use a QSpinBox instead. I did find a thread on how to implement a Hexidecimal SpinBox, but unfortunately the link to the third-party widget is dead. However it does say you could:
subclass QSpinBox and reimplement textFromValue() and valueFromText() to show hexadecimal values.
With the right Decimal-to-Hexidecimal function (you don't mention what language you are using) this would be a suitable solution.
The other alternative, is to use the QLineEdit.setValidator function with a custom subclass of QValidator to provide a validation method. For this, just re-implement the QValidator.validate function to check what a "valid" value.
To build on Lego Stormtroopr's answer, I think QValidator is a good and simple option. For example
dialog->lineedit1->setValidator(new QRegExpValidator( QRegExp("[0-9]{1,20}"), this ));
Here, the QRegExp denotes that you shall only accept digits from 0 to 9 and no other key-presses (spaces or letters) and you shall accept atleast 1 and maximum 20 characters (digits). You then set your lineedit's validator to this value. For more information, you can visit http://qt-project.org/doc/qt-5.0/qtcore/qregexp.html
or for a double,
QDoubleValidator *myDblVal = new QDoubleValidator( 0.0, MAX_VALUE, 1, this);
myDblVal->setNotation( QDoubleValidator::StandardNotation );
dialog->lineedit1->setValidator( myDblVal );
Here you simply use the inbuilt Qt functionality for double validation. You shall only accept a decimal between 0 and MAX_VALUE.
What I need
ok I googled this and there are many tutorials on how to get the charCode from the character but I cant seem to find out how to get the character from the charcode.
Basically I am I am listening for the KeyDown event on a TextInput.
I prevent the char from being typed via event.preventDefault();
Later I need to add the text-char to the TextInput.
I can get the charCode via event.charCode so if I can turn that into a string I can save it for later user.
Why I need it
Basically I am making a TextInput, that that I can set to display default text in it. When A user types into it, I want to remove the default text first then add the user typed text.
Currently I am either removing it all, or ending up with both.
It's simple:
var yourNewChar:String = String.fromCharCode(event.charCode);
If I add a listener to KeyboardEvent.KEY_DOWN, I can find out the keyCode and the charCode.
The keyCode maps to a different character depending on the keyboard.
The charCode is just as useless, according to the help:
The character code values are English keyboard values. For example, if you press Shift+3, charCode is # on a Japanese keyboard, just as it is on an English keyboard.
So, how can I find out which character the user pressed?
You left out a pretty important part of the quote or it was missing where you found it:
For example, if you press Shift+3, the
getASCIICode() method returns # on a
Japanese keyboard, just as it does on
an English keyboard.
http://livedocs.adobe.com/flex/201/langref/flash/events/KeyboardEvent.html
This is probably more helpful:
The charCode property is the numeric value of that key in the current character set (the default character set is UTF-8, which supports ASCII).
http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000480.html
Your application determines what characters set is used, meaning that the even if you have to use separate keys of different keyboard locals to produce the same character, it will have the same charCode.
NOTE: (This is about keyboard messages in general and does not apply to actionscript alone. I misread the question and provided a deeper answer then was helpful)
Really, the path from keyboard to windows char is a VERY complex one, it goes something like this:
Keyboard send scancode to Keyboard device driver (KDD).
KDD sends a message to the system message queue.
The system then sends the message to the foreground thread that created the window with the current keyboard focus.
The thread's message loop picks up the message and figures out the correct character translation.
The 'real' char that was typed is not calculated until it finishes that whole process, as each window and thread can be on a different locale and you can't really 'translate' the key without knowing the locale and key buffer history.
The "WM_KEYDOWN" and "WM_KEYUP" messages cannot just be converted with MapVirtualKey or something because you don't know how many key presses make up a single char. The simple method is just handle the 'WM_CHAR' event and use that. Consider the following:
en-US locale, you press the following keys a + ' + a, you get the following output "a'a"
pt-BZ locale, you press the following keys a + ' + a, you get the following output "aá"
So in both examples you would get 3 KEYDOWN, KEYUP messages, but in the first you get 3 WM_CHAR and in the second you only get 2.
The following article is really good for the basic concepts:
http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
You cannot effectively use charCode or keyCode to determine the character that was entered. You must compare strings only. The KeyboardEvent does not give you the entered text, which is also silly.
In my case I implemented a KeyboardEvent.KEY_DOWN event in addition to a TextEvent.TEXT_INPUT event. In the handler for the latter I implemented all functionality where the charCode was needed and didn't vary per keyboard locale (eg. space bar or enter). In the the former I checked for the text property of the event to compare what I needed locale independent.
Forgot to mention that this post hinted me to that solution: How to find out the character pressed key in languages?
Typing Japanese hiragana etc characters often require several keystrokes and sometimes even selecting the appropriate character from a drop down menu. You probably want to listen for a different event, something like a textfield's change event.