I have a QLineEdit and I am using it for a measurement conversion application. In that QLineEdit I have to use only integer values, so I used the QDoubleValidator.
q_LineEdit->setValidator(new QDoubleValidator(this));
Now I want the QLineEdit to accept only " and / characters to it, as well as the integers, as it is required for the conversion application. How can I make my QLineEdit accept it while using a QDoubleValidator?
Note: I want my QLineEdit to accept something like this (eg. 70“1/2).
Note: The QLineEdit should not accept any other characters other than " and /.
At last I figured out the answer by myself. It's very simple. Just use QRegExpValidator.
Here's my piece of code:
QRegExp rx("(|\"|/|\\.|[0-9]){30}");
m_LineEdit->setValidator(new QRegExpValidator(rx, this));
Related
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")
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.
I'm trying to display Chinese characters encoded in UTF-8 in a PlainTextEdit control, but it doesn't render them properly.
My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database). Once I have the Chinese character in a QString, I tried various things to display it but always results in either question marks or random ASCII characters:
QString chineseChar = query.value(fieldNo).toString(); // get the character
ui->plainTextEdit->appendPlainText(chineseChar); // doesn't work
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
Any suggestion on how to handle that?
"My data comes from a database and I know that the string I get in Qt is correct (the bytes are the same as in the database)."
How did you check that? Try with chineseChar.toUtf8().toHex().
Once your string data is in a QString, all UI elements accepting a QString will handle it correctly. Usually the error happens when converting from plain text data(const char*/QByteArray) to the QString.
The conversions here:
ui->plainTextEdit->appendPlainText(chineseChar.toUtf8()); // doesn't work
ui->plainTextEdit->appendPlainText(QString::fromUtf8(chineseChar.toAscii()); // doesn't work
convert the unicode string to a bytearray, and then implicitely back to a QString, as those methods expect a QString.
I suggest you define QT_NO_CAST_FROM_ASCII and QT_NO_CAST_TO_ASCII to avoid any unwanted QByteArray<->QString conversions.
If the string is wrong, the error usually happened before, when converting from QByteArray/const char* to QString, i.e. in query.value(fieldNo).toString(). Try with:
QString chineseChar = QString::fromUtf8( query.value(fieldNo).toByteArray() );
If that doesn't help, the problem is somewhere in QtSQL assuming the wrong encoding for the data it receives from the database.
How is it possible to have qlineedit->settext() to accept file of type .ma only?
I assume you want the string to be of the format <filename>.ma in the QLineEdit.
If that's the case, you can use
void QLineEdit::setValidator ( const QValidator * v )
Also, QRegExpValidator can validate for specific strings.
I am not well versed with RegExp but I guess QRegExp rx("*.ma"); as an Regular Expression will be fine for your case.
Hope it helps...
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.