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. Compare QVariant in Qt6
Forum Updated to NodeBB v4.3 + New Features

Compare QVariant in Qt6

Scheduled Pinned Locked Moved Solved General and Desktop
qt6deprecated
6 Posts 4 Posters 1.7k Views 2 Watching
  • 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
    autoantwort
    wrote on last edited by
    #1

    In Qt5 is was possible to compare QVariants, but that functionality is now deprecated: Doc.
    But there is the function bool QMetaType::compare(const void *lhs, const void *rhs, int typeId, int *result) you can use. But the function always return false (no comparison possible). My code:

        QVariant left  = ...;
        QVariant right = ...;
        int res;
        if (!QMetaType::compare(left.data(), right.data(), left.type(), &res)) {
            qWarning() << "Can not compare " << left << " and " << right; // prints: Can not compare  QVariant(int, 0)  and  QVariant(int, 2)
            return 0;
        }
        return res < 0;
    

    What am I doing wrong?

    1 Reply Last reply
    0
    • A autoantwort

      No I want to know whether one is smaller than the other. The old code was simply:

      return left < right;
      
      Y Offline
      Y Offline
      YanisFrt
      wrote on last edited by YanisFrt
      #6

      @autoantwort

      You can Use

      return  QPartialOrdering::Less == QVariant::compare(left, right);
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Do you mean compare them for equality ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

          No I want to know whether one is smaller than the other. The old code was simply:

          return left < right;
          
          Y 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            See https://lists.qt-project.org/pipermail/interest/2020-April/034945.html for a discussion on why it was deprecated.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply
            2
            • Christian EhrlicherC Christian Ehrlicher

              See https://lists.qt-project.org/pipermail/interest/2020-April/034945.html for a discussion on why it was deprecated.

              A Offline
              A Offline
              autoantwort
              wrote on last edited by autoantwort
              #5

              @Christian-Ehrlicher I understand the reasons why they deprecate it. I have the same use-case as described there. So a solution seems to be:

              bool SortedModelVectorView::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const{
                  const auto left = ....;
                  const auto right = ....;
                  switch (left.type()) {
                  case QVariant::Int: return left.value<int>() < right.value<int>();
                  case QVariant::Bool: return left.value<bool>() < right.value<bool>();
                  case QVariant::String: return left.value<QString>() < right.value<QString>();
                  }
                  qWarning() << "Can not compare " << left << " and " << right << ". Type " << left.type() << " not handled";
                  return 0;
              

              Where you have to implement all types that you will handle. Ok this is one solution, but still why says QMetaType::compare that a comparison is not possible?

              1 Reply Last reply
              0
              • A autoantwort

                No I want to know whether one is smaller than the other. The old code was simply:

                return left < right;
                
                Y Offline
                Y Offline
                YanisFrt
                wrote on last edited by YanisFrt
                #6

                @autoantwort

                You can Use

                return  QPartialOrdering::Less == QVariant::compare(left, right);
                
                1 Reply Last reply
                1

                • Login

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