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. How to use QStandardItem subclass to sort by Chinese order
Qt 6.11 is out! See what's new in the release blog

How to use QStandardItem subclass to sort by Chinese order

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 667 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.
  • A Offline
    A Offline
    Aizzzz
    wrote on last edited by Aizzzz
    #1

    Hello,
    Another sort question. I have a treeview to sort by Chinese phonetic alphabet, and i use a subclass of QStandardItem and reimplement operator'<' like Assitant told me, the code like:

    class MyOwnTreeItem: public QStandardItem
    {
    public:
        bool operator<(const MyOwnTreeItem&other) const
        {
            const int role = model() ? model()->sortRole() : Qt::DisplayRole;
            const QVariant l = data(role), r = other.data(role);
            QLocale local(QLocale::Chinese);
            QCollator collator(local);
            return collator.compare(l.toString(), r.toString());
        };
    }
    

    insert in model

    void MyOwnTreeModel::insertItem
    {
        MyOwnTreeItem*pItem = new MyOwnTreeItem();
        /* add item data */
        this->appendRow(pItem);
    }
    

    and I call it like:

    void MyOwnTreeWidget::sortItem
    {
        QList<QStandardItem *> itemList = pTreeModel->findItems("*", Qt::MatchWildcard | Qt::
            MatchRecursive);
    
    /*  find the parent item need to sort */
    
        MyOwnTreeItem*pTreeItem = static_cast<MyOwnTreeItem*>(pParentItem);
        pTreeItem->sortChildren(0, Qt::AscendingOrder);
    }
    

    but not works, can anyone tell me what's wrong of my code?

    JonBJ 1 Reply Last reply
    0
    • A Aizzzz

      Hello,
      Another sort question. I have a treeview to sort by Chinese phonetic alphabet, and i use a subclass of QStandardItem and reimplement operator'<' like Assitant told me, the code like:

      class MyOwnTreeItem: public QStandardItem
      {
      public:
          bool operator<(const MyOwnTreeItem&other) const
          {
              const int role = model() ? model()->sortRole() : Qt::DisplayRole;
              const QVariant l = data(role), r = other.data(role);
              QLocale local(QLocale::Chinese);
              QCollator collator(local);
              return collator.compare(l.toString(), r.toString());
          };
      }
      

      insert in model

      void MyOwnTreeModel::insertItem
      {
          MyOwnTreeItem*pItem = new MyOwnTreeItem();
          /* add item data */
          this->appendRow(pItem);
      }
      

      and I call it like:

      void MyOwnTreeWidget::sortItem
      {
          QList<QStandardItem *> itemList = pTreeModel->findItems("*", Qt::MatchWildcard | Qt::
              MatchRecursive);
      
      /*  find the parent item need to sort */
      
          MyOwnTreeItem*pTreeItem = static_cast<MyOwnTreeItem*>(pParentItem);
          pTreeItem->sortChildren(0, Qt::AscendingOrder);
      }
      

      but not works, can anyone tell me what's wrong of my code?

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

      @Aizzzz said in How to use QStandardItem subclass to sort by Chinese order:

      but not works

      In what way "not works"? Does not get called? Gets called but does not do any sorting at all? Does sorting but does not respect the order you expect for Chinese? I would put a debug breakpoint in your bool operator< and see if that returns what you expect on each call.

      A 1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        @Aizzzz said in How to use QStandardItem subclass to sort by Chinese order:

        bool operator<(const MyOwnTreeItem&other) const {
        ...
        QCollator collator(local);
        return collator.compare(l.toString(), r.toString());
        }
        

        The function needs to return true if the this item is less than other, and false otherwise. QCollator::compare() does not return a bool, but the result will be converted: 0 becomes false, anything else true. So if this item equals other then false else true.

        1 Reply Last reply
        1
        • JonBJ JonB

          @Aizzzz said in How to use QStandardItem subclass to sort by Chinese order:

          but not works

          In what way "not works"? Does not get called? Gets called but does not do any sorting at all? Does sorting but does not respect the order you expect for Chinese? I would put a debug breakpoint in your bool operator< and see if that returns what you expect on each call.

          A Offline
          A Offline
          Aizzzz
          wrote on last edited by
          #4

          @JonB
          It's do sorting but does not as expected order. The result order is default order (seems be Utf-8 encode order, Google tell me the GB encode will order as I expected), even if I try return true in bool operator<, its still sort by the default order. Now it looks like operator < is not called, still use QStandardItem::operator<(const QStandardItem &other) const.
          And the Visual Studio 2017 tell me the breakpoint not avaliable :(

          JonBJ 1 Reply Last reply
          0
          • A Aizzzz

            @JonB
            It's do sorting but does not as expected order. The result order is default order (seems be Utf-8 encode order, Google tell me the GB encode will order as I expected), even if I try return true in bool operator<, its still sort by the default order. Now it looks like operator < is not called, still use QStandardItem::operator<(const QStandardItem &other) const.
            And the Visual Studio 2017 tell me the breakpoint not avaliable :(

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

            @Aizzzz
            bool QStandardItem::operator<(const QStandardItem &other) const is the signature and I see it is marked virtual. Make sure your signature matches exactly. Add the keyword override at the end of your subclassed declaration. This will warn you if you have not got the correct exact declaration.

            A 1 Reply Last reply
            1
            • JonBJ JonB

              @Aizzzz
              bool QStandardItem::operator<(const QStandardItem &other) const is the signature and I see it is marked virtual. Make sure your signature matches exactly. Add the keyword override at the end of your subclassed declaration. This will warn you if you have not got the correct exact declaration.

              A Offline
              A Offline
              Aizzzz
              wrote on last edited by
              #6

              @JonB

              Oh, indeed my declaration is wrong, it should be bool operator<(const QStandardItem&other) const instead of
              bool operator<(const MyOwnTreeItem&other) const, what a stupid mistake :(

              JonBJ 1 Reply Last reply
              3
              • A Aizzzz

                @JonB

                Oh, indeed my declaration is wrong, it should be bool operator<(const QStandardItem&other) const instead of
                bool operator<(const MyOwnTreeItem&other) const, what a stupid mistake :(

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

                @Aizzzz
                That is why I would suggest you always add override any time you are intending to override a virtual method, then you will always know if you have got it right! :)

                1 Reply Last reply
                4

                • Login

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