extends QString : QString « Qt « C++






extends QString

  


#include <QList>
#include <QtAlgorithms>   // for qSort()
#include <QStringList>
#include <QDebug>

class CaseIgnoreString : public QString {
public:
    CaseIgnoreString(const QString& other = QString()) : QString(other) {}

    bool operator<(const QString & other) const {
        return toLower() < other.toLower();
    }
    bool operator==(const QString& other) const {
        return toLower() == other.toLower();
    }
};

int main() {
    CaseIgnoreString s1("A"), s2("b"), s3 ("C"), s4("d"), s5 ("D");
    QList<CaseIgnoreString> namelist;

    namelist << s5 << s1 << s3 << s4 << s2;
 
    qSort(namelist.begin(), namelist.end());
    int i=0;
    foreach (QString stritr, namelist) {
        qDebug() << QString("namelist[%1] = %2").arg(i++).arg(stritr) ;
    }

    QStringList strlist;
    strlist << s5 << s1 << s3 << s4 << s2; 

    qSort(strlist.begin(), strlist.end());
    qDebug() << "StringList sorted: " + strlist.join(", ") << endl;
    return 0;
}

   
    
  








Related examples in the same category

1.Complete demo for QString
2.Using QString as the parameter
3.Format command line parameter with QString
4.cpp with qstring
5.Qt type size