I want to update my code from old style casting to new style one. I have some problems with understanding the different type of casts.
1 case
if (((QDataItem*)(*it))->GetType()==QDataItem::AudioTrack){
Here I have a class "QDataItem". This contain an enum of track types, like AudioTrack. Basing on a QTreeWidget I iterate through the QTreeWidget items. Each Item represents a QDataItem. Now with new casting I want to do:
if ((static_cast<QDataItem*>(*it))->GetType()==QDataItem::AudioTrack){
Is this there right way to do?
2 case
In old style I have a twice casting
QAudioTrackItem *audio_track = (QAudioTrackItem*)(QDataItem*)(*it);
QAudioTrackItem is like QDataItem a class. I want to do here:
QAudioTrackItem *audio_track = reinterpret_cast<QAudioTrackItem*>(*it)
But I am not sure that this is correct in case of the missing QDataItem cast.
Is my result ok or do I have a bug?
Is this there right way to do?
Yes, (QDataItem*)(*it) and static_cast<QDataItem*>(*it) should be identical with your code.
But if your classes have such inheritance structure:
class QDataItem : QObject {};
class QAudioTrackItem : QDataItem {};
you should really consider using qobject_cast<>() instead:
if (auto item = qobject_cast<QAudioTrackItem *>(*it)) {
....
}
Is my result ok or do I have a bug?
Maybe, it depends on how your inheritance structure look like.
If they inherit just like my example above, it's totally OK to use reinterpret_cast<>() to convert any pointers between QObject, QDataItem and QAudioTrackItem.
But if your classes have multiple inheritance:
class QDataItem {};
class QAudioTrackItem : QObject, QDataItem {};
reinterpret_cast could kick your ass badly:
auto item = new QAudioTrackItem;
auto p1 = reinterpret_cast<QObject *>(item); // OK
auto p2 = reinterpret_cast<QAudioTrackItem *>(p1); // OK
auto p3 = reinterpret_cast<QDataItem *>(item); // Undefined Behavior
auto p4 = reinterpret_cast<QDataItem *>(p1); // Undefined Behavior
Related
Hi im relatively new to c# code and i was wondering if there is any way to get the amount of columns and rows in a grid and store that amount in a variable
Something like:
var columnamount = grid.columnamount;
But i could not find anything that works
Thanks
You can use the following code to get a count of the columns and rows directly via the ColumnDefinitions and RowDefinitions properties. No need to enumerate the children of the grid because you may not have views in every column/row.
var columnCount = grid.ColumnDefintions.Count;
var rowCount = grid.RowDefinitions.Count;
For reference the documentation.
You might be able to do it this way, purely based on what I see in the docs:
var countColumns = grid.Children.Where( c => c.Column).Max();
var countRows = grid.Children.Where( c => c.Row).Max();
But I'm not sure if you can access Row anf Column properties on the child element.
This is not the best way to check, I guess, but it's working (same thing for columns):
EDIT: nope, for columns it doesn't work
int GetRowsCount(Grid grid)
{
var item = grid.Children.FirstOrDefault();
return item != null ? Grid.GetRow(item) + 1 : 0;
}
I have an ArrayCollection(neList) of Objects(neObj). Each neObj has several fields like ipAddress,TID,etc.. In most cases neObj will be have values of both TID and ipAddress, rarely it will not have TID but have ipAddress... After adding Objects(neObj), I need to sort the ArrayCollection whose behaviour must be similar to array.sort() which has got strings only..(i.e nos first followed by strings in alphabetical order)
Things I have tried:
1)Using neList.source.sort() and neList.refresh.. but it did not work as neList.source has objects not straight forward things like strings
2)I think i cannot use sortOn function of ArrayCollection as it can be done on only 1 field
My Requirement:
Use Case1:- Objects in ArrayCollection have both TID and IP
neObj1.TID="RAPC" neObj1.ipAddress="121.1.1.2"; neObj2.TID="RAPA" neObj2.ipAddress="121.1.1.1"
O/P after sorting should be
neObj2 neObj1
Use Case2:- 1 of the objects does not have TID
neObj1.ipAddress="121.1.1.2"; neObj2.TID="RAPA" neObj2.ipAddress="121.1.1.1"
O/P after sorting should be
neObj1 neObj2
As hinted in the comments, you'll need to use a sort compareFunction to decide how the items will be sorted.
I do like to point out that sorting a combination of letters and numbers is tricky in the sense that there is no natural order by default. e.g. when sorting, 1, 2 and 11, the order will be 1, 11, 2. You can however solve this using the naturalCompare method in the AS3Commons Lang project.
Here's a code sample for your case. The sort is implemented as a subclass of the Sort class so that you can easily reuse it in other collections:
package {
import mx.collections.Sort;
import org.as3commons.lang.StringUtils;
public class NaturalSort extends Sort {
public function NaturalSort() {
compareFunction = function (a:Object, b:Object, fields:Array = null):int {
var stringA:String = (("TID" in a) ? a.TID : "AAAA") + a.ipAddress;
var stringB:String = (("TID" in b) ? b.TID : "AAAA") + b.ipAddress;
return StringUtils.naturalCompare(stringA, stringB);
};
}
}
}
To apply this:
var collection:ArrayCollection;
collection.sort = new NaturalSort();
collection.refresh();
How can I set the inner HTML in a QDomElement?
When I’m using QWebElement I have the method QWebElement::setInnerXml, there is some similar method in QDomElement?
There's no API to "inject" XML snippets as text into a QDomDocument (or QXmlStreamWriter). One has to use the API and create the nodes programmatically.
Assuming you have a string to start with, my current solution is to generate a DOM tree from it, import that tree in a fragment, then copy those in the right place (you must have an import which is why you need an intermediate fragment. Quite unfortunate if you ask me.)
// assuming that 'n' is the node you are replacing or at least inserting after
// 'parent' is the parent of 'n'
// 'result' is the string to replace 'n' or insert after 'n'
QDomDocument doc_text("snap");
doc_text.setContent("<text>" + result + "</text>", true, NULL, NULL, NULL);
QDomDocumentFragment frag(xml.createDocumentFragment());
frag.appendChild(xml.importNode(doc_text.documentElement(), true));
QDomNodeList children(frag.firstChild().childNodes());
const int max(children.size());
QDomNode previous(n);
for(int i(0); i < max; ++i)
{
QDomNode l(children.at(0));
parent.insertAfter(children.at(0), previous);
previous = l;
}
// if you are replacing, then delete node n as well
parent.removeChild(n);
Note that the <text> tag is used so that way result does not need to be a tag, it could just be text and it will still work.
Obviously, if you have a fragment or XML from another document to start with, ignore the code that creates that code in the doc_text object.
I have a model of type QStandardItemModel which looks like this:
QHash<int, QByteArray> roleNames;
roleNames[Car2goVehicle::NameRole] = "plate_number";
roleNames[Car2goVehicle::DescriptionRole] = "address";
roleNames[Car2goVehicle::FuelRole] = "fuel";
roleNames[Car2goVehicle::InteriorRole] = "interior";
roleNames[Car2goVehicle::ExteriorRole] = "exterior";
roleNames[Car2goVehicle::VinRole] = "vin";
roleNames[Car2goVehicle::LatRole] = "lat";
roleNames[Car2goVehicle::LonRole] = "lon";
roleNames[Car2goVehicle::DistanceRole] = "distance";
d->m_vehiclesmodel = new RoleItemModel(roleNames);
and now I want to sort according to distance like this
d->m_vehiclesmodel->setSortRole(Qt::UserRole);
d->m_vehiclesmodel->sort(Car2goVehicle::DistanceRole, Qt::AscendingOrder);
But the result is wrong. Can somebody tell me how to sort ?
Thanks.
What's wrong with the result?
In most cases, one doesn't sort the model itself, but the view, using a QSortFilterProxyModel. Here's the example from the documentation:
QTreeView *treeView = new QTreeView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
treeView->setModel(proxyModel);
In your example above, you might mix up roles and columns. Your Role enum should look like this:
enum Role {
NameRole=Qt::UserRole,
DistanceRole,
...
};
If you want to sort by distance role, you call:
model->setSortRole( Car2goVehicle::DistanceRole );
Then, sort by some column (which has nothing to do with the role), E.g. column 0:
model->sort( 0, Qt::AscendingOrder );
I wouldn't recommend using QSortFilterProxyModel, if QStandardItemModel::sort() is powerful enough. Instead, I stick use basic Qt signals.
Here, I'm using PyQt, but the code should also work in C++:
self.model = QStandardItemModel()
self.populate_model()
self.model.setHorizontalHeaderLabels(map(str, range(self.model.rowCount())))
self.treeview.setModel(self.model)
self.treeview.header().setSectionsClickable(True)
self.treeview.header().setSortIndicatorShown(True)
self.treeview.header().sortIndicatorChanged.connect(self.model.sort)
Notice the last line connecting QHeaderView::sortIndicatorChanged with QStandardItemModel::sort.
How do I resolve the error of duplicate variable definitions? There has to be
separate namespaces and use for each definition, but I'm just not seeing it.
CODE
I didn't write this, but I've been trying to unpackage it and change the classes and seem to have broken it. I want to use this for time-scaling the playback of my movies.There's cool math in here for time-scaling.
//time-scaling script
import flash.display.*;
import flash.events.Event.*;
var _time_scale:Number = .25;
var _frames_elapsed:int = 0;
var _clip:MovieClip;
function Main():void {
_clip = new SomeClip;
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
//integer??
function handleEnterFrame(e:Event):void {
_frames_elapsed ++;
}
// we multiply the "real" time with our timescale to get the scaled time
// we also need to make sure we give an integer as a parameter, so we use Math.round() to round the value off
_clip.gotoAndStop(Math.round(_clip.totalFrames * _frames_elapsed * _time_scale ));
}
var myTimer:Timer = new Timer(10);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
function timerListener (e:TimerEvent):void{
ball1.rotationY += 5;/////////replace function///////////
}
myTimer.start();
ERRORS
**3596**
Warning: Duplicate variable definition.
**1151**
A conflict exists with definition _clip in namespace internal
NOTES
integers, non nested loop
It's because you are missing the ending "}" of the constructor, after this line:
addEventListener(Event.ENTER_FRAME, handleEnterFrame);
And the two following lines should probably be in your constructor, not just in the class declaration:
var myTimer:Timer = new Timer(10);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
If you are using the Timer and TimerEvent classes, you should import them:
import flash.utils.Timer;
import flash.events.TimerEvent;
Also, you don't need the .* at the end of the Event import.
Another "also". You should have access modifiers on your members ie. vars, and functions. So you should really say:
private var _clip:MovieClip;
It sounds to me like you need to look into the basics of AS3. Here is a really good starting point: http://www.actionscript.org/resources/articles/611/1/Getting-started-with-Actionscript-3/Page1.html
_clip is a reserved key word, you'll have to use something else.