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. QList wrapper
Forum Update on Monday, May 27th 2025

QList wrapper

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlist
13 Posts 5 Posters 985 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.
  • F Offline
    F Offline
    firen
    wrote on 25 Nov 2022, 18:25 last edited by firen
    #1

    Hi,

    I would like to override QList.

    If I instantiate it for example with QString, I would write:

    QList<QString>

    and if I would like to use Pointer it would be

    QList<QString*>.

    So is there a chance that I could override QList, that an instantiation QList<QString> would work like QList<QString*>? I would ALWAYS like to store/work only with the pointers.

    Thanks!

    C M 2 Replies Last reply 25 Nov 2022, 18:34
    0
    • F firen
      25 Nov 2022, 18:25

      Hi,

      I would like to override QList.

      If I instantiate it for example with QString, I would write:

      QList<QString>

      and if I would like to use Pointer it would be

      QList<QString*>.

      So is there a chance that I could override QList, that an instantiation QList<QString> would work like QList<QString*>? I would ALWAYS like to store/work only with the pointers.

      Thanks!

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 25 Nov 2022, 18:34 last edited by
      #2

      @firen said in QList wrapper:

      So is there a chance that I could override QList, that an instantiation QList<QString> would work like QList<QString*>? I would ALWAYS like to store/work only with the pointers.

      No, and why do you need to store a pointer to a QString in a container? Happy memleaks...

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • F firen
        25 Nov 2022, 18:25

        Hi,

        I would like to override QList.

        If I instantiate it for example with QString, I would write:

        QList<QString>

        and if I would like to use Pointer it would be

        QList<QString*>.

        So is there a chance that I could override QList, that an instantiation QList<QString> would work like QList<QString*>? I would ALWAYS like to store/work only with the pointers.

        Thanks!

        M Offline
        M Offline
        mpergand
        wrote on 25 Nov 2022, 18:58 last edited by mpergand
        #3

        @firen
        A template class ?

        template <class T>
        class PointerList : public QList<T*>
        {
            public:
                       PointerList() : QList<T*>() {}
                       ~PointerList()
                            {
                            qDeleteAll(this->begin(),this->end());
                            }
        
            private:
                     Q_DISABLE_COPY(PointerList)
        
        };
        
        F 1 Reply Last reply 26 Nov 2022, 12:23
        0
        • M mpergand
          25 Nov 2022, 18:58

          @firen
          A template class ?

          template <class T>
          class PointerList : public QList<T*>
          {
              public:
                         PointerList() : QList<T*>() {}
                         ~PointerList()
                              {
                              qDeleteAll(this->begin(),this->end());
                              }
          
              private:
                       Q_DISABLE_COPY(PointerList)
          
          };
          
          F Offline
          F Offline
          firen
          wrote on 26 Nov 2022, 12:23 last edited by firen
          #4

          @mpergand That was my idea as well but didn*t work. maybe I did something wrong. I will try again. :-)

          I read about Q_DISABLE_COPY. Isn`t it mainly for QObject based types?

          C M 2 Replies Last reply 26 Nov 2022, 12:35
          0
          • F firen
            26 Nov 2022, 12:23

            @mpergand That was my idea as well but didn*t work. maybe I did something wrong. I will try again. :-)

            I read about Q_DISABLE_COPY. Isn`t it mainly for QObject based types?

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 26 Nov 2022, 12:35 last edited by
            #5

            @firen said in QList wrapper:

            I read about Q_DISABLE_COPY. Isn`t it mainly for QObject based types?

            If you want to copy PointerList <T> then you have to provide a proper copy operator, the one generated by the compiler will not work, therefore Q_DISABLE_COPY.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • F firen
              26 Nov 2022, 12:23

              @mpergand That was my idea as well but didn*t work. maybe I did something wrong. I will try again. :-)

              I read about Q_DISABLE_COPY. Isn`t it mainly for QObject based types?

              M Offline
              M Offline
              mpergand
              wrote on 26 Nov 2022, 13:48 last edited by
              #6

              @firen said in QList wrapper:

              That was my idea as well but didn*t work. maybe I did something wrong. I will try again. :-)

              Show your code.
              You must add dynamically allocated objects to the list, it seems obvious but maybe it needs to be said.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                firen
                wrote on 28 Nov 2022, 09:07 last edited by firen
                #7

                It looks like this now:

                template <class T>
                class CPtrListWrapper:
                  public QList<T*>
                {
                
                  using Item = T*;
                
                public:
                  CPtrListWrapper():
                    QList(),
                    m_bAutoDelete(false),
                    m_pCurrentItem(nullptr)
                  {
                    
                  }
                   
                  ~CPtrListWrapper()
                  {
                
                  }
                
                //I need to wrap an old Containerclass and want to replace it internally by QList and these is one of the original methods
                  void insert(uint i,
                    const T* d) // 
                  {
                    QList::insert(i, d);
                  }
                

                But this gives me an error C2664 if I try:

                bas::qtw::CPtrList<QString> m_q5List;
                const QString* pString = new QString("TEST");
                
                m_q5List.insert(0, pString);
                
                

                The error is that he cannot convert "const QString *" in "QString *const &"

                J 1 Reply Last reply 28 Nov 2022, 09:10
                0
                • F firen
                  28 Nov 2022, 09:07

                  It looks like this now:

                  template <class T>
                  class CPtrListWrapper:
                    public QList<T*>
                  {
                  
                    using Item = T*;
                  
                  public:
                    CPtrListWrapper():
                      QList(),
                      m_bAutoDelete(false),
                      m_pCurrentItem(nullptr)
                    {
                      
                    }
                     
                    ~CPtrListWrapper()
                    {
                  
                    }
                  
                  //I need to wrap an old Containerclass and want to replace it internally by QList and these is one of the original methods
                    void insert(uint i,
                      const T* d) // 
                    {
                      QList::insert(i, d);
                    }
                  

                  But this gives me an error C2664 if I try:

                  bas::qtw::CPtrList<QString> m_q5List;
                  const QString* pString = new QString("TEST");
                  
                  m_q5List.insert(0, pString);
                  
                  

                  The error is that he cannot convert "const QString *" in "QString *const &"

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 28 Nov 2022, 09:10 last edited by jsulm
                  #8

                  @firen said in QList wrapper:

                  But this gives me an error C2664 if I try:

                  Please post the whole error, not just its id.
                  Also, show the declaration of m_q5List.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  F 1 Reply Last reply 28 Nov 2022, 09:13
                  0
                  • J jsulm
                    28 Nov 2022, 09:10

                    @firen said in QList wrapper:

                    But this gives me an error C2664 if I try:

                    Please post the whole error, not just its id.
                    Also, show the declaration of m_q5List.

                    F Offline
                    F Offline
                    firen
                    wrote on 28 Nov 2022, 09:13 last edited by
                    #9

                    @jsulm I have edited the post.

                    Thanks! :)

                    J 1 Reply Last reply 28 Nov 2022, 09:26
                    0
                    • F firen
                      28 Nov 2022, 09:13

                      @jsulm I have edited the post.

                      Thanks! :)

                      J Offline
                      J Offline
                      JonB
                      wrote on 28 Nov 2022, 09:26 last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        firen
                        wrote on 28 Nov 2022, 12:53 last edited by firen
                        #11
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          firen
                          wrote on 28 Nov 2022, 13:01 last edited by
                          #12
                          This post is deleted!
                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mpergand
                            wrote on 28 Nov 2022, 13:25 last edited by
                            #13
                                PointerList<QString> plist;
                                plist.append(new QString("World"));
                                plist.insert(0,new QString("Hello"));
                            
                                for(const QString* str : plist)
                                    qDebug()<<*str;
                            

                            print:
                            "Hello"
                            "World"

                            1 Reply Last reply
                            0

                            5/13

                            26 Nov 2022, 12:35

                            topic:navigator.unread, 8
                            • Login

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