PDA

View Full Version : Storing Data in Models using QVariants or Non-variant Types



Goug
2nd December 2011, 05:34
I've got a model/view set up and for the most part, it's working pretty well, but I'm uncertain about one of the choices I made. It occured to me that constantly converting to QVariant in the data method for visual updates was probably more expensive than storing the data in QVariant's inside the model and then just returning the data as-is. The downside is that I keep making mistakes external to the model. In a few cases, I've changed the base data type of a column, but then I have to find all of the places where I extract the data and fix up the "toInt", "toString" or whatever.

I know that Qt does some pretty incredible things to improve performance, and I'm wondering if I'm shooting myself in the foot worrying about the QVariant conversion. My code would be more reliable using non-variant types in the model because my property methods would change types and the compiler would flag most everything that needs to be fixed up.

Thoughts?

Thanks,
Doug

d_stranz
5th December 2011, 01:46
I think I would store non-variant types in the model and not worry about the conversions. The views used by Qt are pretty smart - they will usually only ask for data that is actually displayed, and there is only so much you can show at one time.

Where you are likely to take a hit is in proxy models where you must sort the model before you can display it. If you have to repeatedly convert from a QVariant into some native type in order to do sort order comparisons, this could be very expensive.

Goug
5th December 2011, 20:50
Thanks -- I hadn't thought about the sorting aspect yet, and I do need that (probably starting on that today). I'd been leaning towards using the native types anyway, and this confirms my gut feeling. Appreciate the help!

Doug