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

QList wrapper

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlist
13 Posts 5 Posters 1.1k Views 1 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.
  • F firen

    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!

    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 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

      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 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
      0
      • M mpergand

        @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 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?

        Christian EhrlicherC M 2 Replies Last reply
        0
        • F firen

          @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?

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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

            @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 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 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 &"

              jsulmJ 1 Reply Last reply
              0
              • F firen

                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 &"

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                0
                • jsulmJ jsulm

                  @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 last edited by
                  #9

                  @jsulm I have edited the post.

                  Thanks! :)

                  JonBJ 1 Reply Last reply
                  0
                  • F firen

                    @jsulm I have edited the post.

                    Thanks! :)

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #10
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      firen
                      wrote on last edited by firen
                      #11
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • F Offline
                        F Offline
                        firen
                        wrote on last edited by
                        #12
                        This post is deleted!
                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mpergand
                          wrote on 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

                          • Login

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