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. Why does not QListWidget sort correctly?

Why does not QListWidget sort correctly?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 764 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.
  • Q Offline
    Q Offline
    Qingshui Kong
    wrote on last edited by
    #1

    Hi everybody,

    Could somebody give me some help?
    I want to use QListWidget sortItems() function. I enabled sortingEnable. But I don't think the result is correct.
    If I input items 1, 2, 1152, the result will be 1, 1152, 2. But I think the result should be 1, 2, 1152.

    And if I test this code,
    if("1" == "1152")
    {
    qDebug() << "'1' == '1152'";
    }
    else if("1" > "1152")
    {
    qDebug() << "'1' > '1152'";
    }
    else
    {
    qDebug() << "'1' < '1152'";
    }
    if("2" == "1152")
    {
    qDebug() << "'2' == '1152'";
    }
    else if("2" > "1152")
    {
    qDebug() << "'2' > '1152'";
    }
    else
    {
    qDebug() << "'2' < '1152'";
    }
    The output is
    '1' < '1152'
    '2' < '1152'

    I think this is right.

    So could somebody give me some advice?
    Thanks in advance.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Do you know that you are comparing their memory addresses?
      If you want to test the comparing, you should write like this

      if(QString("1") == QString("1152"))
      

      And the result is

      '1' < '1152'
      '2' > '1152'
      

      That is how string comparision works.
      It will not compare the number value...

      One workaround to this is that do not use setText, but

      item->setData(Qt::DisplayRole, 1151);
      
      Q 1 Reply Last reply
      4
      • B Bonnie

        Do you know that you are comparing their memory addresses?
        If you want to test the comparing, you should write like this

        if(QString("1") == QString("1152"))
        

        And the result is

        '1' < '1152'
        '2' > '1152'
        

        That is how string comparision works.
        It will not compare the number value...

        One workaround to this is that do not use setText, but

        item->setData(Qt::DisplayRole, 1151);
        
        Q Offline
        Q Offline
        Qingshui Kong
        wrote on last edited by
        #3

        @Bonnie Thank you very much.
        You explained how string comparision works. That is correct. And I tested as you said. But in my opinion, I still think they should compare the length of each string. Let it alone.

        At first my code is
        ui->listWidget->addItem(QString::number(nNum));
        ui->listWidget->sortItems();
        It works. I can see the num in the listwidget.

        But after I changed the code to
        QListWidgetItem item;
        item.setData(Qt::DisplayRole, nNum);
        ui->listWidget->addItem(&item);
        ui->listWidget->sortItems();
        It doesn'tt work. I can't see anything.

        VRoninV JonBJ 2 Replies Last reply
        0
        • Q Qingshui Kong

          @Bonnie Thank you very much.
          You explained how string comparision works. That is correct. And I tested as you said. But in my opinion, I still think they should compare the length of each string. Let it alone.

          At first my code is
          ui->listWidget->addItem(QString::number(nNum));
          ui->listWidget->sortItems();
          It works. I can see the num in the listwidget.

          But after I changed the code to
          QListWidgetItem item;
          item.setData(Qt::DisplayRole, nNum);
          ui->listWidget->addItem(&item);
          ui->listWidget->sortItems();
          It doesn'tt work. I can't see anything.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          @Qingshui-Kong said in Why does not QListWidget sort correctly?:

          But after I changed the code to
          QListWidgetItem item;
          item.setData(Qt::DisplayRole, nNum);
          ui->listWidget->addItem(&item);
          ui->listWidget->sortItems();

          You were very close to the solution, you just have to create the QListWidgetItem on the heap instead of the stack otherwise it gets destroyed:

          auto item = new QListWidgetItem;
          item->setData(Qt::DisplayRole, nNum);
          ui->listWidget->addItem(item);
          ui->listWidget->sortItems();
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Q 1 Reply Last reply
          2
          • Q Qingshui Kong

            @Bonnie Thank you very much.
            You explained how string comparision works. That is correct. And I tested as you said. But in my opinion, I still think they should compare the length of each string. Let it alone.

            At first my code is
            ui->listWidget->addItem(QString::number(nNum));
            ui->listWidget->sortItems();
            It works. I can see the num in the listwidget.

            But after I changed the code to
            QListWidgetItem item;
            item.setData(Qt::DisplayRole, nNum);
            ui->listWidget->addItem(&item);
            ui->listWidget->sortItems();
            It doesn'tt work. I can't see anything.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @Qingshui-Kong said in Why does not QListWidget sort correctly?:

            But in my opinion, I still think they should compare the length of each string.

            Then you had better let the C++/Qt designers know you don't agree with the way string comparisons work.

            Q 1 Reply Last reply
            0
            • VRoninV VRonin

              @Qingshui-Kong said in Why does not QListWidget sort correctly?:

              But after I changed the code to
              QListWidgetItem item;
              item.setData(Qt::DisplayRole, nNum);
              ui->listWidget->addItem(&item);
              ui->listWidget->sortItems();

              You were very close to the solution, you just have to create the QListWidgetItem on the heap instead of the stack otherwise it gets destroyed:

              auto item = new QListWidgetItem;
              item->setData(Qt::DisplayRole, nNum);
              ui->listWidget->addItem(item);
              ui->listWidget->sortItems();
              
              Q Offline
              Q Offline
              Qingshui Kong
              wrote on last edited by
              #6

              @VRonin Thanks. After I responded. I tried that. It worked.

              1 Reply Last reply
              0
              • JonBJ JonB

                @Qingshui-Kong said in Why does not QListWidget sort correctly?:

                But in my opinion, I still think they should compare the length of each string.

                Then you had better let the C++/Qt designers know you don't agree with the way string comparisons work.

                Q Offline
                Q Offline
                Qingshui Kong
                wrote on last edited by
                #7

                @JonB Maybe I just made a mistake. I just think I saw something like that.

                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