Problem with QVariant/QTreeWidgetItem/iterator on qt4.4.3 - linux

In my qt app I have this object, filled before setting up my QTreeWidget's content:
QList<QTreeWidgetItem*> items;
I fill the QList by this way:
QVariant qv; // I need this for "attaching" to the item my linuxPackage object
qv.setValue(linuxPackage);
packRow->setData(1, Qt::UserRole,qv); // packRow is my own object inherited from QTreeWidgetItem, I "put" the QVariant into it
items.append(packRow); // then I put my item into the QList
at the end of the work, my QList has almost 1000 items.
I need to iterate over them and for each item I need to get the "linuxPackage" data by this (tested and working) way:
Pkg linuxPackage = this->data(1,Qt::UserRole).value<Pkg>(); // Pkg is my own class for the linuxPackage object
So, I've been trying to extract needed data by this manner:
QList<QTreeWidgetItem*>::iterator iter;
for (iter = items.begin(); iter != items.end(); ++iter){
Pkg pack = iter->data(1,Qt::UserRole).value<Pkg>();
}
But nothing works, I can not even get the program compiling. Help! :D

Perhaps:
(*iter)->data(1,Qt::UserRole).value<Pkg>();
BTW, an easier way of doing this with Qt4:
foreach (const QTreeWidgetItem *item, items) {
Pkg pack = item->data(1,Qt::UserRole).value<Pkg>();
}
at the very least, you should use const_iterators =)
QList<QTreeWidgetItem*>::const_iterator iter;
for (iter = items.constBegin(); iter != items.constEnd(); ++iter){
...
}

Related

tinyxml2 XMLElement constructor private?

In TinyXml you could create an Element e.g. TiXmlElement("tag"),
but in TinyXml2 there is no public constructor for XMLElement?
How do you create elements ?
Similar to the existing answer, I wrote this helper utility for my application:
tinyxml2::XMLElement* CChristianLifeMinistryEntry::InsertNewElement(tinyxml2::XMLDocument& rDoc, tinyxml2::XMLElement*& pParent, LPCSTR strElement, CString strValue)
{
XMLElement *pElement = rDoc.NewElement(strElement);
USES_CONVERSION;
if (pElement == nullptr)
AfxThrowMemoryException();
pElement->SetText(CT2CA(strValue, CP_UTF8));
pParent->InsertEndChild(pElement);
return pElement;
}
It automatically adds a new child element to the end of the list. In addition, it sets the text value for the element.
You create an element within the context of the document, so call
tinyxml2::XMLElement * tinyxml2::XMLDocument::NewElement (const char * name).
E.g. to create a new element and add it as a child of an existing element e
XMLElement * new = e -> GetDocument() -> NewElement ("tag");
e -> InsertFirstChild (new);
Or, to do it in a single step, you could look up append_element in my tinyxml2 extension

How to make the return false if the arraylist already have the string present in class?

I'm new to coding.
How do I return a false if there is a string being added that's already in the arraylist?
For example, if you have a list of dog names in the class and you add new dog names in the list, but don't add it when the same dog name was already in the list?
The Solution:
You could use a for statement to iterate through your array list:
public static bool checkArray(string dogName)
{
for int i=0; i<arrayName.Length; i++) // basic for loop to go through whole array
{
if (arrayName[i] == dogName) //checks if array value at index i is the dog's name
{
return true; //if it is, return true
}
}
return false; //gone through whole array, not found so return false
}
This means you can call your method via
string Name = "myDogsName";
bool isAlreadyPresent = checkArray(Name);
Note
This is written in C#, and so other coding languages will slightly
differ in their syntax.
isAlreadyPresent will then contain a bool value if the dog is
present or not
I have written this (for learning purposes) in (possibly) an
inefficient way, but should allow you to understand what is happening
at each stage.
the i++
The i++ may confuse new programmers, but effectively it is the same as writing
i = i + 1;
This also works for i--;
i = i - 1;
Or even i*=2;
i = i * 2;

Priority Queue object comparator with hashset object

HashSet Object ct_set City Object
how can i initialize a ProrityQueue object ct_pq with elements in ct_set with order from my populatation comparator
Create the PriorityQueue with your Comparator, and then just call addAll:
HashSet<City> cities = ...;
PriorityQueue<City> queue = new PriorityQueue(new CityComparator());
queue.addAll(cities);
Note that if you've really got a HashSet<Object> instead (your question is far from clear) you should probably try to change your code so that you do have a HashSet<City> instead. Or you can always just cast each element:
HashSet<Object> cities = ...;
PriorityQueue<City> queue = new PriorityQueue(new CityComparator());
for (Object x : cities) {
queue.add((City) x);
}

Linq to split/analyse substrings

I have got a List of strings like:
String1
String1.String2
String1.String2.String3
Other1
Other1.Other2
Test1
Stuff1.Stuff1
Text1.Text2.Text3
Folder1.Folder2.FolderA
Folder1.Folder2.FolderB
Folder1.Folder2.FolderB.FolderC
Now I would like to group this into:
String1.String2.String3
Other1.Other2
Test1
Stuff1.Stuff1
Text1.Text2.Text3
Folder1.Folder2.FolderA
Folder1.Folder2.FolderB.FolderC
If
"String1" is in the next item "String1.String2" I will ignore the first one
and if the second item is in the third I will only take the third "String1.String2.String3"
and so on (n items). The string is structured like a node/path and could be split by a dot.
As you can see for the Folder example Folder2 has got two different Subfolder items so I would need both strings.
Do you know how to handle this with Linq? I would prefer VB.Net but C# is also ok.
Regards Athu
Dim r = input.Where(Function(e, i) i = input.Count - 1 OrElse Not input(i + 1).StartsWith(e + ".")).ToList()
Condition within Where method checks if element is last from input or is not followed by element, that contains current one.
That solution uses the fact, that input is List(Of String), so Count and input(i+1) are available on O(1) time.
LINQ isn't really the correct approach here, because you need to access more than one item at a time.
I would go with something like this:
public static IEnumerable<string> Filter(this IEnumerable<string> source)
{
string previous = null;
foreach(var current in source)
{
if(previous != null && !current.Contains(previous))
yield return previous;
previous = current;
}
yield return previous;
}
Usage:
var result = strings.Filter();
Pretty simple one. Try this:
var lst = new List<string> { /*...*/ };
var sorted =
from item in lst
where lst.Last() == item || !lst[lst.IndexOf(item) + 1].Contains(item)
select item;
the following simple line can do the trick, I'm not sure about the performance cost through
List<string> someStuff = new List<string>();
//Code to the strings here, code not added for brewity
IEnumerable<string> result = someStuff.Where(s => someStuff.Count(x => x.StartsWith(s)) == 1);

Java ME ArrayList - Vector - Define object types and access object methods through vector

I'm making a shopping list mobile application (Java ME) and i have two classes; item, list.
item object allows get/set name and quantity (string itemName, int quantity)
Now i need to store an array of items in my list class and be able to access the methods of the object from its list array index as follows; code below is pseudo code
item[] itemList = new item[]
for(int x = 0; x < itemList.length; x++)
{
String tempStoreOfName = itemList[x].getItemName()
System.out.println(tempStoreOfName)
}
I've googled a lot and found out that you can use vectors however i cannot seem to be able to call the object's methods. Where am i going wrong?
I've done something like this in C# and i used ArrayLists however these are not supported in Java ME
Current Code
Vector itemList = new Vector();
for(int x = 0; x <= itemList.size(); x++)
{
dataOutputStream.writeUTF(itemList.elementAt(x)*here i cannot put .getItemName()*);
}
Since you can't use generics you have to cast so that Java knows what you got out of the Vector. Notice that Vector.elementAt() returns Object? all you can do with it is treat it like an Object:
item myItem = itemList.elementAt(n);
fails because java can't auto-cast to a more specific class. You'd have to use:
Object myItem = itemList.elementAt(n);
which is useless to you because you want an item, not an Object.
You have to cast it to an object of the type you want:
for(...)
{
item myItem = (item) itemList.elementAt(n);
myItem.method();
}
From then on you just use myItem.

Resources