Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert QVector of QSharedPointer to Qvector of QWeakPointer

Convert QVector of QSharedPointer to Qvector of QWeakPointer

Scheduled Pinned Locked Moved General and Desktop
containerssmart pointers
3 Posts 2 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • TheBadgerT Offline
    TheBadgerT Offline
    TheBadger
    wrote on last edited by
    #1

    Hi,

    In an attempt to make use of smart pointers, I have a class that contains a QVector with QSharedPointers. Is there an easy way to convert this vector to a vector of QWeakPointers (instead of one by one)?

    My use case is:
    I have a class that should manage the lifetime of my objects, but calling classes would like to get access to this list of objects and interact with them temporarily as long as the pointers are valid (up-cast to QSharedPointers).


    Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      QVector<QSharedPointer<Type>> and QVector<QWeakPointer<Type>> are unrelated types so there's no "all at once" conversion.
      But you don't even need an explicit loop to do it, so it's not a hard thing:

      QVector<QSharedPointer<T>> v1 { /* ... */ }
      QVector<QWeakPointer<T>> v2;
      
      v2.reserve(v1.size());
      std::transform(v1.begin(), v1.end(), std::back_inserter(v2), 
                     [](const QSharedPointer<T>& ref) { return ref.toWeakRef(); });
      
      TheBadgerT 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        QVector<QSharedPointer<Type>> and QVector<QWeakPointer<Type>> are unrelated types so there's no "all at once" conversion.
        But you don't even need an explicit loop to do it, so it's not a hard thing:

        QVector<QSharedPointer<T>> v1 { /* ... */ }
        QVector<QWeakPointer<T>> v2;
        
        v2.reserve(v1.size());
        std::transform(v1.begin(), v1.end(), std::back_inserter(v2), 
                       [](const QSharedPointer<T>& ref) { return ref.toWeakRef(); });
        
        TheBadgerT Offline
        TheBadgerT Offline
        TheBadger
        wrote on last edited by
        #3

        Thanks I was expecting as much.
        Also thanks for the code, I will probably replace my foreach with something similar.


        Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved