Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] How to sort a QVector of QPair?



  • Hello,

    I have a QVector with a QPair as Type, like this:
    @QVector<QPair<int, Modul*> > *listWidgets;@

    Modul is my own class and inherite from QGroupBox

    Now I liked to sort my QVector by int value. I tried this:
    @
    bool sorting(const QPair<int, Modul*> &e1, const QPair<int, Modul*> &e2)
    {
    if(e1.first < e2.first)
    return true;
    }

    qsort(listWidgets->begin(), listWidgets->end(), sorting);
    @

    But I get an error:
    @
    error: invalid conversion from 'QVector<QPair<int, Modul*> >::iterator {aka QPair<int, Modul*>*}' to 'size_t {aka unsigned int}' [-fpermissive]
    qsort(listWidgets->begin(), listWidgets->end(), sorting);@

    Could somebody help me? What I am doing wrong?



  • Isnt using a QMap better for your use? It will do the sorting for you.
    It seems that you are converting a QVector iterator to an unsigned int value. That will surely not be the thing you want. The QVector iterator will more or less be a pointer.





  • Ahh thanks for the hint man. Now I am using a QMultiMap which is a perfect solution for my problem.


Log in to reply