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. [SOLVED] How to sort a QVector of QPair?

[SOLVED] How to sort a QVector of QPair?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 5.2k 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.
  • H Offline
    H Offline
    heiopei
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Ildar
        wrote on last edited by
        #3

        "qsort":http://qt-project.org/doc/qt-5/qtalgorithms-obsolete.html is obsolete method, use "sort":http://www.cplusplus.com/reference/algorithm/sort/?kw=sort function instead

        1 Reply Last reply
        0
        • H Offline
          H Offline
          heiopei
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

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