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. supporting QStringList::join for classes
Forum Update on Monday, May 27th 2025

supporting QStringList::join for classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 321 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.
  • P Offline
    P Offline
    petrytar
    wrote on 18 Jul 2019, 15:49 last edited by
    #1

    Hello,

    I was wondering if there is any option (override some methods from QString for example) other then derive from it directly that will allow create list of objects and calls join method on them.
    E.g.

    QList<MyClass> l{ob1, ob2, ob3}; 
    QString ls = l.join("\n");
    

    Where MyClass could be represented as a QString.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Jul 2019, 16:06 last edited by
      #2

      Hi
      Not really.
      Im not sure what you need it for ?

      You can make a toString() in your class and do
      QList<MyClass> l{ob1.toString(), ob2.toString(), ob3.toString()};

      But if its for debugging there are better options :)

      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 18 Jul 2019, 18:28 last edited by
        #3

        a bit buried inside QList but it's doable. What you need to do is define a template class:

        template <> struct QListSpecialMethods<MyClass>{
        protected:
            ~QListSpecialMethods() {}
        public:
        
        };
        

        Now in the public part you can implement the methods you want (join included). For example:

        class MyClass{
        public:
            MyClass(int val = 0)
                :m_value(val)
            {}
            int value() const {return m_value;}
        private:
            int m_value;
        };
        
        template <> struct QListSpecialMethods<MyClass>{
        protected:
            ~QListSpecialMethods() {}
        public:
            int join(int startValue) const{
                const QList<MyClass>* const self = static_cast<const QList<MyClass>*>(this);
                return std::accumulate(self->cbegin(),self->cend(),startValue,[](int a, const MyClass& b)->int{return a+b.value();});
            }
        };
        
        int main(int argc, char **argv)
        {
            QCoreApplication app(argc,argv);
            QList<MyClass> myClassList;
            myClassList.append(MyClass(1));
            myClassList.append(MyClass(2));
            myClassList.append(MyClass(3));
            qDebug() << myClassList.join(4);
            return app.exec();
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        3

        3/3

        18 Jul 2019, 18:28

        • Login

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