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. QMap of QScopedPointer
Forum Updated to NodeBB v4.3 + New Features

QMap of QScopedPointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.0k Views 2 Watching
  • 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.
  • P Offline
    P Offline
    Pavel Romankov
    wrote on last edited by Pavel Romankov
    #1

    Hello
    Can i create a QMap of QScopedPointer?
    For example:

    QMap<int, QScopedPointer<MyClass>> myClassObjects;
    
    for (int i = 1; i <= 10; i++) {
       myClassObjects.insert(i, QScopedPointer<MyClass>(new MyClass()));
    }
    

    when i make so, i have compile error. What is the right way to do this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      No you can't. QScopePointer cannot be copied.

      Why do you want such a map ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      P 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        No you can't. QScopePointer cannot be copied.

        Why do you want such a map ?

        P Offline
        P Offline
        Pavel Romankov
        wrote on last edited by
        #3

        @SGaist
        For example: i have modbus port and need modbus device. I use QScopedPointer fot it.

        QScopedPointer<QModbusRtuSerialMaster> modbusDevice;
        

        But now I want to use some ports, so I need N devises. I decided to store them in QMap, but as i see, it is wrong way)

        M 1 Reply Last reply
        0
        • P Pavel Romankov

          @SGaist
          For example: i have modbus port and need modbus device. I use QScopedPointer fot it.

          QScopedPointer<QModbusRtuSerialMaster> modbusDevice;
          

          But now I want to use some ports, so I need N devises. I decided to store them in QMap, but as i see, it is wrong way)

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @Pavel-Romankov
          Since it descends from QObject, no need to do more then that:

          auto device=new QModbusRtuSerialMaster(this);

          P 1 Reply Last reply
          0
          • M mpergand

            @Pavel-Romankov
            Since it descends from QObject, no need to do more then that:

            auto device=new QModbusRtuSerialMaster(this);

            P Offline
            P Offline
            Pavel Romankov
            wrote on last edited by
            #5

            @mpergand
            it was example.
            I use my own class, not descended from QObject, and store it in context which not QObject too.

            M 1 Reply Last reply
            0
            • P Pavel Romankov

              @mpergand
              it was example.
              I use my own class, not descended from QObject, and store it in context which not QObject too.

              M Offline
              M Offline
              mpergand
              wrote on last edited by
              #6

              @Pavel-Romankov said in QMap of QScopedPointer:

              I use my own class, not descended from QObject, and store it in context which not QObject too.

              Why not ?

              Anyway, to delete all the container you can use:
              qDeleteAll(map.begin(), map.end());

              P 1 Reply Last reply
              0
              • M mpergand

                @Pavel-Romankov said in QMap of QScopedPointer:

                I use my own class, not descended from QObject, and store it in context which not QObject too.

                Why not ?

                Anyway, to delete all the container you can use:
                qDeleteAll(map.begin(), map.end());

                P Offline
                P Offline
                Pavel Romankov
                wrote on last edited by
                #7

                @mpergand
                Hm, thank you for idea!)

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  You could also use a copyable smart pointer like QSharedPointer if threading issues are considered.

                  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