{"id":356,"date":"2011-07-22T10:51:35","date_gmt":"2011-07-22T17:51:35","guid":{"rendered":"http:\/\/www.lothlorien.com\/kf6gpe\/?p=356"},"modified":"2011-07-22T10:51:35","modified_gmt":"2011-07-22T17:51:35","slug":"more-qt-tips","status":"publish","type":"post","link":"https:\/\/www.lothlorien.com\/kf6gpe\/more-qt-tips\/","title":{"rendered":"More Qt tips&#8230;"},"content":{"rendered":"<p>Following on the heels of last week&#8217;s post, here are a few more thoughts gleaned from the experience of attending last year&#8217;s Qt Developer Days. These are all old news, especially if you&#8217;ve been doing Qt for a while, but putting them here may be useful to you, and clears out a page of thoughts in my engineering notes!<\/p>\n<p><strong><code>QImage<\/code> vs. <code>QPixmap<\/code><\/strong><br \/>\nRemember that <code>QImage<\/code> is the general-purpose class for image manipulation whose data lives in main memory, while <code>QPixmap<\/code> objects are usually backed by the GPU. This has some ramifications for your application:<\/p>\n<ul>\n<li><code>QImage<\/code> is optimized for I\/O and direct pixel access.<\/li>\n<li><code>QPixmap<\/code> is optimized for direct screen display.<\/li>\n<li>On constrained platforms like mobile devices, there may be relatively little GPU RAM available, so you should consider using <code>QPixmap<\/code> objects sparingly.\n<\/ul>\n<p>Regardless of which you choose, both classes use Qt&#8217;s copy-on-write (COW) semantics, so if you have multiple instances of the same image, you pay a small price penalty&#8212;basically the cost of the object overhead, not the memory footprint of the image itself.<\/p>\n<p><strong><code>QStandardItemModel<\/code> and Performance<\/strong><br \/>\nQt&#8217;s support for model-view-controller is great, and one of the handiest things about it is that the <code>QStandardItemModel<\/code> class provides everything you need in a model to get going. It is, however, not necessarily the most efficient way to do so.<\/p>\n<p>If you have your own model already&#8212;say you&#8217;re porting an existing application, or working with a library with its own data representation&#8212;it really is a good idea to implement your own model class from <code>QAbstractItemModel<\/code>. I&#8217;m not bad-mouthing <code>QStandardItemModel<\/code>, but it&#8217;s based on Qt&#8217;s collection classes, and odds are that you know better ways to store your data (especially if there&#8217;s a lot of it!) than Qt does. <\/p>\n<p>Personally, my approach is that if I don&#8217;t have a data container and I&#8217;m only dealing with a handful of data items &#8212; say, up to a hundred or so objects with a half-dozen roles or so &#8212; I use <code>QStandardItemModel<\/code> and don&#8217;t look back. (Qt&#8217;s collection classes are, after all, quite good for general applications). But if I&#8217;ve already got a data model, or I know I&#8217;m going to be dealing with lots and lots of objects, it&#8217;s time for a custom model that leverages my knowledge of the data set I&#8217;m wrapping.<\/p>\n<p><strong>Use <code>QtConcurrent<\/code> for Parallel Computation<\/strong><br \/>\nI&#8217;ve not tried this personally, but <code>QtConcurrent<\/code> looks <em>really cool<\/em> for doing any kind of concurrent map\/reduce style programming. If you need to write code that leverages multiple cores for concurrent computation, it&#8217;s best to look here before trying to roll your own framework using <code>QThread<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following on the heels of last week&#8217;s post, here are a few more thoughts gleaned from the experience of attending last year&#8217;s Qt Developer Days. These are all old news, especially if you&#8217;ve been doing Qt for a while, but putting them here may be useful to you, and clears out a page of thoughts &hellip; <a href=\"https:\/\/www.lothlorien.com\/kf6gpe\/more-qt-tips\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">More Qt tips&#8230;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,9],"tags":[],"class_list":["post-356","post","type-post","status-publish","format-standard","hentry","category-mobile","category-programming"],"_links":{"self":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/comments?post=356"}],"version-history":[{"count":6,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/356\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/posts\/356\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/media?parent=356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/categories?post=356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lothlorien.com\/kf6gpe\/wp-json\/wp\/v2\/tags?post=356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}