qt5_8_snippets.html qt5_snippets.html


Snippets from/for Qt5.3 & 4.8

Nur ein paar wichtige Klassen und Methoden, die ich immer mal brauche - hier als Erinnerungsstütze...

QList

TODO ...


QVector

Example

QStringList l;

l << "Siegfried" << "Gunther" << "Hagen" << "Krimhild";

QVector v = QVector::fromList( l );// v is now ["Siegfried","Gunther","Hagen","Krimhild"].


Example

std::vector vStd;

vStd.push_back(1.2);

vStd.push_back(0.5);

vStd.push_back(3.14);

QVector v = QVector::fromStdVector( vStd );

see also toStdVector() and QList::fromStdList().


Example

QVector vector;

vector << "X" << "Y" << "Z" << "Y" << "X";

vector.indexOf("Y"); // returns 1

vector.indexOf("Y", 1); // returns 1

vector.indexOf("Y", 2); // returns 3

vector.indexOf("A"); // returns -1

int QVector::indexOf(const T & value, int from = 0) const



QMap

const Key key(const T & value, const Key & defaultKey = Key()) const

QList keys() const

QList keys(const T & value) const

const T value(const Key & key, const T & defaultValue = T()) const

QList values() const

QList values(const Key & key) const

std::map toStdMap() const

QList uniqueKeys() const

void swap(QMap & other)

T take(const Key & key) or int remove(const Key & key)


Beispiel

QMap mapString2Int;

mapString2Int["sero"] = 0;

mapString2Int["three"] = 3;

mapString2Int["seven"] = 7;

mapString2Int.insert("nine, 9);


int iNum3 = mapString2Int["three"];//iNum3 is 3.

int iNum7 = mapString2Int.value("seven");//iNum7 is 7.

If there is no item with the specified key in the map, these functions return a default-constructed value.

Anti-Example

QMap map;

for( int i = 0; i < 1000; ++i ) if( map[i] == okButton )cout << "Found button at index " << i << endl;

It is better to use contains() and value() rather than operator[]() for looking up a key in a map.

The reason is that operator[]() silently inserts an item into the map if no item exists with the same key

(unless the map is const). In that example 1000 items will be created, what was not the purpose.


Example

Java-style iterator

STL-style iterator

QMapIterator it( map );

while( it.hasNext() ) {
it.next();
cout << it.key() << ": " << it.value() << endl; }

QMap::const_iterator it = map.constBegin();

while( it != map.constEnd() ) {
cout << it.key() << ": " << it.value() << endl;
++it; }


Example

map.insert("contrast", 10);

map.insert("contrast", 200);// map.value("contrast") is now 200.

Default for QMap is one value per key. If one call insert() with a key that already exists in the map, the previous value will be erased.


Example

QList values = map.values("contrast");

for (int i = 0; i < values.size(); ++i)cout << values.at(i) << endl;

Multiple values per key are possible with insertMulti() instead of insert() and if one want to retrieve all the values for a single key, one can use values(const Key &key), which returns a QList.


Example

find

foreach

QMap::iterator it = map.find( "contrast" );

while (it != map.end() && it.key() == "contrast") {

cout << it.value() << endl;

++it;

}}

QMap map;

...

foreach (int value, map)

cout << value << endl;


QMultiMap

Example

insert() function corresponds to QMap::insertMulti()

values(const Key &key) returns a QList

STL-style iterator and find

QMultiMap map1, map2, map3;

map1.insert("contrast", 11);

map1.insert("contrast", 42);// map1.size() == 2

map2.insert("contrast", 5000);// map2.size() == 1

map3 = map1 + map2;// map3.size() == 3

QList values = map.values("contrast");

for (int i = 0; i < values.size(); ++i)

cout << values.at(i) << endl;

QMultiMap::iterator i = map.find("contrast");

while (i != map.end() && i.key() == "contrast") {

cout << i.value() << endl;

++i;

}


QHash

QHash provides very similar functionality to QMap, but

  • QHash provides faster lookups than QMap.
  • When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
  • The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash().

  • class Person

    {

    public:

    Person() {}

    Person(const QString &name, const QDate &dtBirth);

    //setter and getter ...

    private:

    QString m_sName;

    QDate m_dtBirth;

    };

    inline bool operator==(const Person &p0, const Person &p1)

    { return p0.name() == p1.name() && p0.getDateOfBirth() == p1.getDateOfBirth(); }

    inline uint qHash(const Person &key, uint x)

    { return qHash(key.name(), x) ^ key.getDateOfBirth().day(); }


    QMultiHash

    TODO ...


    QVariant

    TODO ...


    QVariantList

    TODO ...


    QString

    contains, startsWith, endsWith, indexOf, split, simplified, toUtf8, toLatin1, toHtmlEscaped ...


    QStringList

    append, +, << :

    QStringList gallier;

    gallier << Obelix << Asterix << Idefix << Miraculix;

    join :

    QString sGallier = gallier.join(", ");

    sGallier ausgeben oder debuggen: "Obelix, Asterix, Idefix, Miraculix".


    QByteArray (TODO)


    QCache

    skizziert...von Qt\Qt5.2.0\5.2.0\msvc2010_opengl\examples\svg\embedded\fluidlauncher\pictureflow.cpp :

    QCache imageCache;

    ...

    QImage PictureFlowPrivate::slide(int index) const

    {

    return slideImages[index];

    }

    void PictureFlowPrivate::setSlide(int index, const QImage& image)

    {

    if((index >= 0) && (index < slideImages.count()))

    {

    slideImages[index] = image;

    surfaceCache.remove(index);

    triggerRender();

    }

    }

    QImage* PictureFlowPrivate::surface(int slideIndex)

    {

    if(slideIndex < 0)return 0;

    if(slideIndex >= slideImages.count())return 0;

    if(surfaceCache.contains(slideIndex))return surfaceCache[slideIndex];

    QImage img = widget->slide(slideIndex);

    ...