Restricting qlineedit by file type - qt

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...

Related

QLineEdit -- how to enter single value -- with no spaces

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.

Qt Creator setInputMethodHints not work

my main goal right now is to forbid some chars entering in line edit.I want to forbid (!##$%^&*()) chars, cause i do use SQL Database and someone can damage my database without filters for this chars.I tried to use setInputMask but in this way i can use only alphabets.I need to use '-' sign too for some names like "Anna-Maria".
So finally, setInputMethodHints is not working and i don't know what to do.
I just need to forbid some "dangerous" chars.
If you can, please provide me some source code.
Thank you in advance.
Use QRegExpValidator to allow only a-z A-Z 0-9 and '-' character. If you want to add more character just put \charactor in to the rx(".."); like I did with '-' by adding \-
QRegExp rx("[a-zA-Z0-9\-]*");
ui.lineEdit->setValidator(new QRegExpValidator(rx,ui.textEdit));

QLineEdit accepting only integers and ” and / characters?

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));

Displaying UTF-8 characters in a PlainTextEdit

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.

Regex to limit string length for strings with new line characters

Looks like a simple task - get a regex that tests a string for particular length:
^.{1,500}$
But if a string has "\r\n" than the above match always fails!
How should the correct regex look like to accept new line characters as part of the string?
I have a <asp:TextBox TextMode="Multiline"> and use a RegularExpressionValidator to check the length of what user types in.
Thank you,
Andrey
You could use the RegexOptions.Singleline option when validating input. This treats the input as a single line statement, and parses it as such.
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
This should work in multiline inputs.
Can you strip the line breaks before checking the length of the string? That'd be easy to do when validating server-side. (In .net you could use a custom validator for that)
From a UX perspective, though, I'd implement a client-side 'character counter' as well. There's plenty to be found. jQuery has a few options. Then you can implement the custom validator to only run server-side, and then use the character counter as your client-side validation. Much nicer for the user to see how many characters they have left WHILE they are typing.
The inability to set the RegexOptions is screwing you up here. Since this is in a RegularExpressionValidator, you could try setting the options in the regular expression itself.
I think this should work:
(?s)^.{1,500}$
The (?s) part turns on the Singleline option which will allow the dot to match every character including line feeds. For what it's worth, the article here also lists the other RegexOptions and the notation needed to set them as an inline statement.

Resources