Results 1 to 8 of 8

Thread: QTL and erase()

  1. #1
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default QTL and erase()

    Hi,

    I am currently porting c++ code from STL to QTL (std::vector to QVector). QTL claims to be largely compatible to the STL - and it is indeed.
    The one STL method we frequently use and that the QTL doesnt have is

    erase(...)

    Instead the QTL version is called

    remove(...)


    is there any reason behind that?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,344
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTL and erase()

    Different programmers.

    It really isn't necessary to convert perfectly good code written using the STL to use QTL equivalents. When I ported code from MFC over to Qt, I left all of the STL-based code as is, and it works just fine. That way, I avoided problems like you are experiencing, where the two libraries have different naming or use conventions.

  3. #3
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default Re: QTL and erase()

    Some qt apis can only works with QTL containers, most notably serialization via QDataStream().

    the QTL programmers have gone through a lot of effort to provide API-compatibility with the STL (and the STL is using wired naming conventions at times...), but they missed this very obvious one. Wired.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: QTL and erase()

    But instead altering your whole code base you could write some wrappers e.g. with QVector::fromStdVector().

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QTL and erase()

    I am currently porting c++ code from STL to QTL (std::vector to QVector).
    As other already suggested, this is not required. STL based code will work 100% along with Qt framework based code.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,344
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTL and erase()

    As other already suggested, this is not required. STL based code will work 100% along with Qt framework based code.
    I guess the point we are all trying to make is that by porting solid, debugged code from STL to QTL just for the sake of "Qt purity" is likely to introduce new bugs. It is an example of the old saying: "If it isn't broken, don't fix it".

    If the old code is buggy or inefficient, then porting might make sense because it gives you the opportunity to find and fix the bugs or make it work better. But just going through with global search and replace to change std::vector< double > into QVector< double > doesn't improve anything and is likely to make things worse when QTL doesn't act the same way as STL and you don't know why.

    In my case, I have libraries that must be usable in both Qt and non-Qt applications. Sticking with STL in my core algorithms guarantees me not only cross-platform portability, but also independence from Qt where I need it.

  7. #7
    Join Date
    Jul 2012
    Posts
    248
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    29
    Thanked 15 Times in 14 Posts

    Default Re: QTL and erase()

    i am not doing this to every stl container in my code; only very specific ones because only QTL containers can easily be serialized via QDataStream().

    , most notably serialization via QDataStream().
    I am not in for "Qt-purity", excuse my poor phrasing.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,344
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: QTL and erase()

    , most notably serialization via QDataStream().
    The operator<<( QDataStream &, const QVector<T> & ) and operator>>( QDataStream &, QVector<T> & ) streaming operators are not members of the QVector<T> template class, they are external non-member template functions. You could quite easily implement these same operators that would act on the std::vector<T> class and achieve all the benefits of QDataStream without requiring any conversion of your existing STL code:

    Qt Code:
    1. template < typename T >
    2. QDataStream & operator<<( QDataStream & s, const std::vector<T> & v )
    3. {
    4. s << v.size();
    5. std::vector<T>::const_iterator it = v.begin();
    6. std::vector<T>::const_iterator eit = v.end();
    7. while ( it != eit )
    8. s << *it++;
    9. return s;
    10. }
    11.  
    12. template < typename T >
    13. QDataStream & operator>>( QDataStream & s, std::vector<T> & v )
    14. {
    15. size_t nItems;
    16. s >> nItems;
    17. v.resize( nItems );
    18.  
    19. std::vector<T>::iterator it = v.begin();
    20. std::vector<T>::iterator eit = v.end();
    21. while ( it != eit )
    22. s >> *it++;
    23. return s;
    24. }
    To copy to clipboard, switch view to plain text mode 

    And the like for any STL container class. Of course, operator>>() and operator<<() must be defined for whatever T is in the container, along with a default constructor for T.
    Last edited by d_stranz; 30th January 2013 at 21:30. Reason: Changed operator>>() to use iterators

Similar Threads

  1. Replies: 4
    Last Post: 22nd September 2011, 20:37
  2. How to clear or erase QwtPlotcurve?
    By kishorer in forum Qwt
    Replies: 1
    Last Post: 7th September 2011, 08:03
  3. How to erase everything widget has painted?
    By TheNewGuy in forum Newbie
    Replies: 1
    Last Post: 12th December 2009, 08:23
  4. Erase menubar
    By lixo1 in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 17:04
  5. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 21:17

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.