Obtaining text from a QListView - string

I have a pointer to a third party QListView object, which is simply displaying rows of text. What is the best way of getting a hold of that string of text?

The model, accessible by QListView::model(), holds the items. You can do something like this:
QListView* view ; // The view of interest
QAbstractItemModel* model = view->model() ;
QStringList strings ;
for ( int i = 0 ; i < model->rowCount() ; ++i )
{
// Get item at row i, col 0.
strings << model->index( i, 0 ).data( Qt::DisplayRole ).toString() ;
}
You also mention you would like to obtain the updated strings when text is written - you can do this by connecting the model's dataChanged() signal to your function that extracts strings. See QAbstractItemModel::dataChanged().

You can ask the QListView object for its root QModelIndex and use that to iterate over the different entries using the sibling/children methods. You can access the text associated with each index by calling the data method on the index with the role specified as the Qt::DisplayRole.
For more details see the following documentation:
QAbstractItemView - parent class to QListView
QModelIndex

Related

My segmented picker has normal Int values as tags, How is this passed to and from CoreData?

My SwiftUI segmented control picker uses plain Int ".tag(1)" etc values for its selection.
CoreData only has Int16, Int32 & Int64 options to choose from, and with any of those options it seems my picker selection and CoreData refuse to talk to each other.
How is this (??simple??) task achieved please?
I've tried every numeric based option within CoreData including Int16-64, doubles and floats, all of them break my code or simply just don't work.
Picker(selection: $addDogVM.gender, label: Text("Gender?")) {
Text("Boy ♂").tag(1)
Text("?").tag(2)
Text("Girl ♀").tag(3)
}
I expected any of the 3 CoreData Int options to work out of the box, and to be compatible with the (standard) Int used by the picker.
Each element of a segmented control is represented by an index of type Int, and this index therefore commences at 0.
So using your example of a segmented control with three segments (for example: Boy ♂, ?, Girl ♀), each segment is represented by three indexes 0, 1 & 2.
If the user selects the segmented control that represents Girl ♀, then...
segmentedControl.selectedSegmentIndex = 2
When storing a value using Core Data framework, that is to be represented as a segmented control index in the UI, I therefore always commence with 0.
Everything you read from this point onwards is programmer preference - that is and to be clear - there are a number of ways to achieve the same outcome and you should choose one that best suits you and your coding style. Note also that this can be confusing for a newcomer, so I would encourage patience. My only advice, keep things as simple as possible until you've tested and debugged and tested enough to understand the differences.
So to continue:
The Apple Documentation states that...
...on 64-bit platforms, Int is the same size as Int64.
So in the Core Data model editor (.xcdatamodeld file), I choose to apply an Integer 64 attribute type for any value that will be used as an Int in my code.
Also, somewhere, some time ago, I read that if there is no reason to use Integer 16 or Integer 32, then default to the use of Integer 64 in object model graph. (I assume Integer 16 or Integer 32 are kept for backward compatibility.) If I find that reference I'll link it here.
I could write about the use of scalar attribute types here and manually writing your managed object subclass/es by selecting in the attribute inspector Class Codegen = Manual/None, but honestly I have decided such added detail will only complicate matters.
So your "automatically generated by Core Data" managed object subclass/es (NSManagedObject) will use the optional NSNumber? wrapper...
You will therefore need to convert your persisted/saved data in your code.
I do this in two places... when I access the data and when I persist the data.
(Noting I assume your entity is of type Dog and an instance exists of dog i.e. let dog = Dog())
// access
tempGender = dog.gender as? Int
// save
dog.gender = tempGender as NSNumber?
In between, I use a "temp" var property of type Int to work with the segmented control.
// temporary property to use with segmented control
private var tempGender: Int?
UPDATE
I do the last part a little differently now...
Rather than convert the data in code, I made a simple extension to my managed object subclass to execute the conversion. So rather than accessing the Core Data attribute directly and manipulating the data in code, now I instead use this convenience var.
extension Dog {
var genderAsInt: Int {
get {
guard let gender = self.gender else { return 0 }
return Int(truncating: gender)
}
set {
self.gender = NSNumber(value: newValue)
}
}
}
Your picker code...
Picker(selection: $addDogVM.genderAsInt, label: Text("Gender?")) {
Text("Boy ♂").tag(0)
Text("?").tag(1)
Text("Girl ♀").tag(2)
}
Any questions, ask in the comments.

How to access individual values from an NSArray?

Trying to iterate through an array of values coming in from a push notification.
This is what the object in push notification object would look like:
"storyids": [12345, 12346, 12347, 12348]
At which, I can intercept this value in my ios app delegate, as such:
storyIds = aps.ObjectForKey(new NSString("storyids")) as NSArray;
But now I am having trouble iterating through this collection (which I want to essentially convert to a C# collection)
This is what I've tried:
for (nuint i = 0; i < storyIds.Count; i++)
{
var j = storyIds.ValueAt(i);
}
Which gives me a weird number and not the value at that particular index. I can't seem to find any method of NSArray to get the value from the array at a particular index.
You could use storyIds.GetItem<NSString> (0) or NSString.FromHandle (storyIds.ValueAt (0)).

MVC 5 Paging a long list (50 000 object), i used listPaged but it takes time to charge list

I has to display a list of books that containes more than 50 000 book.
I want to display paged list where for each page i invoke a method that gives me 20 books.
List< Books > Ebooks = Books.GetLibrary(index);
But using PagedList doesnt match with my want because it creates a subset of the collection of objects given and accesse to each subset with the index. And refering to the definition of its methode, i had to charge the hole list from the begining.
I also followed this article
var EBooks = from b in db.Books select b;
int pageSize = 20;
int pageNumber = (page ?? 1);
return View(Ebooks.ToPagedList(pageNumber, pageSize));
But doing so, i has to invoke (var Books = from b in db.Books select b; ) on each index
**EDIT****
I'm searching for indications to achieve this
List< Books > Ebooks = Books.GetLibrary(index);
and of course i has the number of all the books so i know the number of pages
So i'm searching for indication that leads me to achieve it: for each index, i invoke GetLibrary(index)
any suggestions ?
Have you tried something like:
var pagedBooks = Books.GetLibrary().Skip(pageNumber * pageSize).Take(pageSize);
This assumes a 0-based pageNumber.
If that doesn't work, can you add a new method to the Books class that gets a paged set directly from the data source?
Something like "Books.GetPage(pageNumber, pageSize);" that way you don't get the entire collection every time.
Other than that, you may have to find a way to cache the initial result of Books.GetLibrary() somewhere.

Binding an edit box within a custom control to a form field programatically

I have a notes form with a series of fields such as city_1, city_2, city_3 etc.
I have an XPage and on that XPage I have a repeat.
The repeat is based on an array with ten values 1 - 10
var repArray = new Array() ;
for (var i=1;i<=10;i++) {
repArray.push(i) ;
}
return(repArray) ;
Within the repeat I have a custom control which is used to surface the fields city_1 through city_10
The repeat has a custom property docdatasource which is passed in
It also has a string custom property called cityFieldName which is computed using the repeat
collection name so that in the first repeat row it is city_1 and in the second it is city_2 etc..
The editable text field on the custom control is bound using the EL formula
compositeData.docdatasource[compositeData.cityFieldName]
This works fine but each time I add new fields I have to remember to create a new custom property and then a reference to it on the parent page.
I would like to be able to simply compute the data binding such as
compositeData.docdatasource['city_' + indexvar]
where indexvar is a variable representing the current row number.
Is this possible ? I have read that you cannot use '+' in Expression Language.
First: you wouldn't need an array for a counter. Just 10 would do (the number) - repeats 10 times too. But you could build an array of arrays:
var repArray = [];
for (var i=1;i<=10;i++) {
repArray.push(["city","street","zip","country","planet"]) ;
}
return repArray;
then you should be able to use
#{datasource.indexvar[0]}
to bind city,
#{datasource.indexvar[1]}
to bind street. etc.
Carries a little the danger of messing with the sequence of the array, if that's a concern you would need to dig deeper in using an Object here.
compute to javascript and use something like
var viewnam = "#{" + (compositeData.searchVar )+ "}"
return viewnam
make sure this is computed on page load in the custom control
I was never able to do the addition within EL but I have been very successful with simply computing the field names outside the custom control and then passing those values into the custom control.
I can send you some working code if you wish from a presentation I gave.

MFC displaying multiple-lines of text in Edit Control box

I am trying to implement a tool that displays file names.
I would like to do this by using SetWindowText() method.
However, When I was trying to use this method in a loop,
the text is displayed in one line and it is continuously refreshed.
here is code snippet
for (int i = 0; i<10; i++)
{
SetWindowText(filenames);
}
please help.! thanks.
SetWindowText replaces the current window text with the string you provide.
So, if you want to show multiple lines with it, you first have to create a multi-line string.
A quick example:
CStringArray names;
// Fill names
CString str;
for (INT_PTR i = 0; i < names.GetCount() ; ++i)
{
str += names[i] + _T("\r\n");
}
c_MyEdit.SetWindowText(str);
Another time-tested method of showing multiple names at once is the list box. MFC provides a nice wrapper with the CListBox Class (see http://msdn.microsoft.com/en-us/library/y04ez4c9%28v=vs.80%29.aspx). This has the added benefit of being scrollable and (optionally) sortable if the list is long.

Resources