[SOLVED] QMap and QVector2D not compatible

  • Hi,

    Is there any way how to implement operator < for QVector2D?
    QMap using operator < to check keys.

    @C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore\qmap.h:67: error: C2678: binary '<' : no operator found which takes a left-hand operand of type 'const QVector2D' (or there is no acceptable conversion)
    c:\qt\qt5.4.0\5.4\msvc2013_64_opengl\include\qtcore\qelapsedtimer.h(80): or 'bool operator <(const QElapsedTimer &,const QElapsedTimer &) throw()'
    c:\qt\qt5.4.0\5.4\msvc2013_64_opengl\include\qtcore\qcollator.h(72): or 'bool operator <(const QCollatorSortKey &,const QCollatorSortKey &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(1424): or 'bool operator <(const QStringRef &,const QStringRef &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(1117): or 'bool operator <(const char *,QLatin1String)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(1060): or 'bool operator <(QLatin1String,QLatin1String)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(652): or 'bool operator <(const char *,const QStringRef &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(645): or 'bool operator <(const char *,const QString &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qstring.h(585): or 'bool operator <(const QString &,const QString &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qbytearray.h(572): or 'bool operator <(const char *,const QByteArray &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qbytearray.h(570): or 'bool operator <(const QByteArray &,const char *)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qbytearray.h(568): or 'bool operator <(const QByteArray &,const QByteArray &)'
    C:\Qt\Qt5.4.0\5.4\msvc2013_64_opengl\include\QtCore/qchar.h(539): or 'bool operator <(QChar,QChar)'
    while trying to match the argument list '(const QVector2D, const QVector2D)'@

    I use QMap this way:
    QMap<QVector2D, float> for Undo action in heightmap editing.
    QVector2D - position
    float - height

    I sort QList values per key and choose first one, other just throw. I used QList and with every edit check if position is already edited, but it was very slow and now trying to figure out fast performance for this action. But seems QMap bit complicate it :/

  • Lifetime Qt Champion


    Rather than QList, you can use QVector which might be faster.

    As for your original problem you would have to provide your implementation of

    @bool operator<(const QVector2D &v1, const QVector2D &v2)@

    in order to work with QMap

  • Yes but this implementation should be inside of QVector2D or in QMap? And do I need edit source code or can I write it in some header file like QDataStream operators <<, >>?

  • Lifetime Qt Champion

    No, it doesn't need to.

    Put it where you are using your QMap

  • Nice, works

  • Lifetime Qt Champion

    Good, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.