Convert user input value into string in QML - qt

I have my function checkPinCode() that will check the user entered pincode with the actual pincode.
Here I am facing a problem to get the user entered input value into string. Since the text field has the pincode as string. So i need to convert the user entered input also to a string during the comparison. How can I convert my pincodeSetting.value to a string. And I am new to QML. It would be nice if some one helps.
function checkPinCode() {
var pinOk = (pinCodeEdit.text === NTModelDBCpp.systemSettings.pincodeSetting.value)
if (false === pinOk) {
pinCodeEdit.text = "";
invalidPinText.visible = true;
}
return pinOk;

move this function(checkPinCode) in your c++ class and do your work in your c++ code
bool NTModelDBCpp::checkPinCode(int pincode){
return pincode === value;
}
and use in qml.the convertion will done automatically.
if(NTModelDBCpp.checkPinCode(yourText){
}
define the checkPinCode as static method in your .h c++ file

Related

Widget SiriKit intent enum value for index

I'm trying to add another SiriKit intent to my Widget to allow user to select a reddit category such as top, hot, new etc. I've set my enum in my custom intents. My question is how do I get the display name from the index? It's being assigned to a URL string so the rawValue won't work. I attempted to use Sort(rawValue:configuration.sort.rawValue)! with the same results https://www.reddit.com/r/swiftui/1.json. I need the url to behttps://www.reddit.com/r/swiftui/hot.json.
I ended up creating an Int enum containing the same as in my intentdefinition. Then created an extension for my enum to return a string value for the intent's rawValue. I was mistaken that my intentdefinition would provide me a string value for the index.
enum SortBy : Int {
case hot = 1
case new
case controversial
case top
case rising
}
extension SortBy {
var sortValue: String {
switch self {
case .hot:
return "hot"
case .new:
return "new"
case .controversial:
return "controversial"
case .top:
return "top"
case .rising:
return "rising"
}
}
}
SortBy(rawValue: configuration.sort.rawValue)!

Get Api flurl.http method used

I am calling a APi using Flurl.http that contains Value and Text field.
I am using that api in Picker !
async void method()
{
string url = "http://xxx.xxx.xxx.xx/api/QMSRejection/GetShiftMaster";
IList<dynamic> list = await url.GetJsonListAsync();
var modelList = new List<string>();
foreach (var item in list)
{
modelList.Add(item.Text);
}
ShiftPicker.ItemsSource = modelList;
}
this is json response api provides
{"Value":"SF2017-1","Text":"General Shift Head Office"},{"Value":"SF2017-2","Text":"Shift-A-1 (Day)"},{"Value":"SF2017-3","Text":"Shift-B-1 (Night)"},{"Value":"SF2017-4","Text":"Shift-C"},{"Value":"SF2017-5","Text":"Shift-9.00AM-6.00PM"},{"Value":"SF201711","Text":"Morning Shift (6.00 Am-3.00Pm)"},{"Value":"SF20171203","Text":"Shift 6:30AM to 3:00PM"},{"Value":"SF2018-1","Text":"General Shift Factory-1"},{"Value":"SF20182","Text":"Shift 10AM to 8PM"},{"Value":"SF20191","Text":"General Shift Factory-2"},{"Value":"SF20192","Text":"Shift 7:00AM to 4:00PM"},{"Value":"SF20193","Text":"Shift-A-2 (Day)"},{"Value":"SF20194","Text":"Shift-B-2 (Night)"}]
I am getting data correctly and I am able to fetch selected Item as well but issue is that I have added only text field but I need access of Value Field as well !
Right now When I have to pass selected value of dropdown, I only have text and I need associated value field with it too so I can use it!
I will display only text but I need access of it !
as #Iria suggested, first define a model class for your data
public class MyClass{
public string Value{get;set;}
public string Text{get;set}
}
then tell flurl to return a strongly typed class instead of using dynamic
var data = await url.GetJsonAsync<List<MyClass>>();
then you can bind your picker directly to data
ShiftPicker.ItemsSource = data;
I would do the following:
var results = await url.GetJsonListAsync();
var myClass = JsonSerializer.Deserialize<IEnumerable<MyClass>>(results);
Then
public class MyClass{
public string Value{get;set;}
public string Text{get;set}
}
in myClass you have a collection of objects that have Value and Text, then you have the correspondance between value and Text for each object

How to create a numeric localized (decimal separator) TextField?

I want to create a numeric TextField which will be localized.
For example, in Excel, if you are in english, if you type a number with the KeyPad, the decimal separator key will enter a dot '.'
But if you are in French, the decimal separator will be a comma ','
Therefore in Excel, the TextField is smart enough to detect in which Locale you're on, and to adapt the decimal separator you actually wanted to print in the TextField.
Right now in JavaFX, I haven't found a way to do that. I was thinking of listening to text modification and to replace any occurrence of dot with a comma if I detect I'm in French. But is this assumption true?
I would recommend subclassing TextField and overriding the insertText and replace* methods. This will handle text entry before the text is updated; listening to text modifications and fixing them will mean you temporarily have "invalid" text in your text field's text property, which could create problems in the rest of your application.
Note that you can create a java.text.DecimalFormatSymbols object for the default (user) Locale (or a specified Locale, if you need), and call getDecimalSeparator() to find the correct decimal separator character. (Similarly, you can call getGroupingSeparator().)
So, for proof of concept, this is nowhere near robust enough for production:
public class NumericTextField extends TextField {
#Override
public void insertText(int index, String text) {
String fixedText = fixText(text);
StringBuilder newText = new StringBuilder(getText().substring(0, index));
newText.append(fixedText);
if (index < getText().length()) {
newText.append(getText().substring(index));
}
if (isValid(newText)) {
super.insertText(index, fixedText);
}
}
#Override
public void replaceText(int start, int end, String text) {
String fixedText = fixText(text);
StringBuilder newText = new StringBuilder(getText().substring(0, start));
newText.append(fixedText);
newText.append(getText().substring(end));
if (isValid(newText)) {
super.insertText(start, end, fixedText);
}
}
#Override
public void replaceText(IndexRange range, String text) {
replaceText(range.getStart(), range.getEnd(), text);
}
private String fixText(String text) {
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
return text.replaceAll("\\.", Character.toString(symbols.getDecimalSeparator()));
}
private boolean isValid(String text) {
// check that the text is a valid numeric representation (or partial representation)
}
}
In the events of a TextField, "setOnKeyPressed" and "setOnKeyReleased", the code of the key pressed can be obtained from the keyEvent and there it can be seen that the key code of the "." pressed from the main keyboard corresponds to the constant "PERIOD", while the keystroke code of "." pressed from the numeric keypad corresponds to the constant "DECIMAL".
From this concept, I share the closest thing I have achieved in this regard, where if I press the "." From the numeric keyboard, according to my specific need, I replace it with the character ",".
totalAmountText.setOnKeyReleased((keyEvent) -> {
if (keyEvent.getCode() == DECIMAL) {
int pos = totalAmountText.getCaretPosition();
totalAmountText.setText(totalAmountText.getText().replace(".", ","));
totalAmountText.positionCaret(pos);
}
});
I am attaching a new version of the code, which takes best practices from the code of the first answer:
totalAmountText.setOnKeyReleased((keyEvent) -> {
if (keyEvent.getCode() == DECIMAL) {
int pos = totalAmountText.getCaretPosition();
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
totalAmountText.setText(totalAmountText.getText().replace(".",
Character.toString(symbols.getDecimalSeparator())));
totalAmountText.positionCaret(pos);
}
});

Flex combo box labelfunction

I have two questions regarding the Flex combo box.
The string representing the function name will be read from xml # run time.
var combo:ComboBox = new ComboBox();
combo.labelFunction = "functionName";
How can I achieve this?
So the first name, which is to be displayed in the combo box, can be only retrieved by accessing another DTO, called person and then its first name.
var combo:ComboBox = new ComboBox();
combo.labelField= "person.firstName";
My class looks like this,
public class Test
{
public var person:PersonDTO;
}
public class PersonDTO
{
public var firstName:String;
}
Is it possible to access any multi-level text using the combo box label field ?
You need to pass the function not the name.
Doing this
combo.labelFunction = "functionName";
Is passing a string.
The only work around I can think of is to make a switch statement with one case for each function you may have. Then call that with "case" from within your xml.
switch( xml.#labelfunction ){
case 'func1':
combo.labelFunction = this.func1;
break;
case 'func2':
combo.labelFunction = this.func2;
break;
}
Its hacky but should work.
ad 1) labelFunction
Calling functions when you know only the name as String is quite easy. The following snippets shows how you can execute a function that is a member of the same class. In case you need to call a function from another class replace this with the according variable name.
private function comboBox_labelFunction(item:Object):String
{
var functionName:String = myXml.#labelFunctionName;
return this[functionName](item);
}
ad 2) labelField
It's normally not possible to use "person.firstName" as labelField. However, you should be able use it within your labelFunction. Something like this should work...
private function comboBox_labelFunction(item:Object):String
{
var labelField:String = "person.firstName";
var attributeNames:Array = labelField.split(".");
for each (var attributeName:String in attributeNames)
{
if (item && item.hasOwnProperty(attributeName))
item = item[attributeName];
else
return null;
}
return item;
}

N2: Set default values for ContentItems

When using N2 CMS:
If I want to set some default values when a new ContentItem is created (e.g. setting the CreatedByUser value for a new Page so I can record who originally created it) where is the best place to put that code?
I figure the constructor of the ContentItem isn't ideal because that will get called when existing objects are loaded.
If you're using the Get/SetDetail syntax then you can do something like this in the property getter:
public virtual string TopImage
{
get { return (string)(GetDetail("TopImage") ?? string.Empty); }
set { SetDetail("TopImage", value); }
}
That's a bit ugly, so there's also an overload for Get/Set detail that lets you specify the default:
public virtual string TopImage
{
get { return GetDetail("TopImage", String.Empty /* Default */); }
set { SetDetail("TopImage", value, String.Empty /* Default */); }
}
If you want to save a value when something is saved then try overriding the AddTo method on the ContentItem. This is called every time the object is saved, so be careful if you only want to call it the first time something is saved (ID == 0 when an Item is "new")

Resources