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. QAbstractItemModel::match bug?
QtWS25 Last Chance

QAbstractItemModel::match bug?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 2.1k 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.
  • K Offline
    K Offline
    Koyi
    wrote on last edited by
    #1

    I have a QCombobox wich model is a QSqlTableModel populated with a DataBase table:
    Integer | String
    =-=-=-=-=-=-=-=
            |
    =-=-=-=-=-=-=-=
        0 | Normal
    =-=-=-=-=-=-=-=
        1 | Leve
    =-=-=-=-=-=-=-=
        2 | Moderado
    =-=-=-=-=-=-=-=
        3 | Severo
    =-=-=-=-=-=-=-=
       
    the combobox works fine
    but when I try to set the value in combo acording with the database table, I use this code

    void MyComboBox::setValor(QVariant myValue)
    {
    QModelIndexList myFinded=this->model()->match(this->model()->index(0,0),Qt::DisplayRole,myValue,5,Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap));
    
    this->setCurrentIndex(myFinded[0].row());
    }
    

    When myValue is 1, myFinded has 1 Index (corresponding to row 2)
    When myValue is 2, myFinded has 1 Index (corresponding to row 3)
    and so on
    but when myValue is 0, myFinded has 2 Index (corresponding to row 0 and 1)
    I don't understand why. Is this a bug? Or am I doing something wrong

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Did you check the content of myValue ?

      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
      • K Offline
        K Offline
        Koyi
        wrote on last edited by
        #3

        Hi
        thanks for a quick answer.
        yes, of course I checked the value of myValue and de value of MyFinded (debug mode).

        Any idea?

        1 Reply Last reply
        0
        • K Offline
          K Offline
          Koyi
          wrote on last edited by
          #4

          I found the problem (not the solution):(

          QAbstractItemModel::match compares each value from the model with the searched value and return the indexes that match.
          it uses the operator== method of QVariant, but it doesn't work fine

          QVariant var1=QVariant(0); // variant containing the integer 0
          
          QSqlQueryModel *miConsulta=new QSqlQueryModel;
          
          miConsulta->setQuery("Select Valor, Descripcion From myTable");
          QVariant var2=miConsulta->record(0).value(0);
          
          

          I get
          var1
          value: 0
          type: QVariant(int)
          isnull: false

          var2
          value:0
          type: QVariant(int)
          isnull: true

          but var1==var2 return TRUE !!!!!!!!!

          how can a null variant be equal to anything?????????

          jeremy_kJ 1 Reply Last reply
          0
          • K Koyi

            I found the problem (not the solution):(

            QAbstractItemModel::match compares each value from the model with the searched value and return the indexes that match.
            it uses the operator== method of QVariant, but it doesn't work fine

            QVariant var1=QVariant(0); // variant containing the integer 0
            
            QSqlQueryModel *miConsulta=new QSqlQueryModel;
            
            miConsulta->setQuery("Select Valor, Descripcion From myTable");
            QVariant var2=miConsulta->record(0).value(0);
            
            

            I get
            var1
            value: 0
            type: QVariant(int)
            isnull: false

            var2
            value:0
            type: QVariant(int)
            isnull: true

            but var1==var2 return TRUE !!!!!!!!!

            how can a null variant be equal to anything?????????

            jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            @Koyi The documentation mentions this:

            "A variant is considered null if it contains a default constructed value or a built-in type instance that has an isNull method, in which case the result would be the same as calling isNull on the wrapped object.

            Warning: The result of the function doesn't affect == operator, which means that two values can be equal even if one of them is null and another is not."

            Asking a question about code? http://eel.is/iso-c++/testcase/

            K 1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              can you consider manual matching?

              
              void MyComboBox::setValor(const QVariant& myValue, int column = 0, int role = Qt::DisplayRole, const QModelIndex& parent=QModelIndex())
              {
              if(myValue.isNull()) return;
              const int rowCount= model()->rowCount(parent);
              for(int i=0;i<rowCount;++i){
              const QVariant currVal = model()->index(i,column,parent ).data(role );
              if(currVal.isNull()) continue;
              if(currVal == myValue)
              return setCurrentIndex(i);
              }
              }
              

              "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

              K 1 Reply Last reply
              2
              • jeremy_kJ jeremy_k

                @Koyi The documentation mentions this:

                "A variant is considered null if it contains a default constructed value or a built-in type instance that has an isNull method, in which case the result would be the same as calling isNull on the wrapped object.

                Warning: The result of the function doesn't affect == operator, which means that two values can be equal even if one of them is null and another is not."

                K Offline
                K Offline
                Koyi
                wrote on last edited by
                #7

                @jeremy_k said in QAbstractItemModel::match bug?:

                @Koyi The documentation mentions this:

                "A variant is considered null if it contains a default constructed value or a built-in type instance that has an isNull method, in which case the result would be the same as calling isNull on the wrapped object.

                Warning: The result of the function doesn't affect == operator, which means that two values can be equal even if one of them is null and another is not."

                You are right.
                I expected that operator== were about

                bool operator==(const QVariant &v1, const QVariant &v2)
                {
                  if (isnull(v1) || isnull(v2))
                  return false;
                .......
                }
                

                because I think a null value cannot be compared with anything.
                Obviously I was wrong.

                1 Reply Last reply
                0
                • VRoninV VRonin

                  can you consider manual matching?

                  
                  void MyComboBox::setValor(const QVariant& myValue, int column = 0, int role = Qt::DisplayRole, const QModelIndex& parent=QModelIndex())
                  {
                  if(myValue.isNull()) return;
                  const int rowCount= model()->rowCount(parent);
                  for(int i=0;i<rowCount;++i){
                  const QVariant currVal = model()->index(i,column,parent ).data(role );
                  if(currVal.isNull()) continue;
                  if(currVal == myValue)
                  return setCurrentIndex(i);
                  }
                  }
                  
                  K Offline
                  K Offline
                  Koyi
                  wrote on last edited by
                  #8

                  @VRonin said in QAbstractItemModel::match bug?:

                  can you consider manual matching?

                  
                  void MyComboBox::setValor(const QVariant& myValue, int column = 0, int role = Qt::DisplayRole, const QModelIndex& parent=QModelIndex())
                  {
                  if(myValue.isNull()) return;
                  const int rowCount= model()->rowCount(parent);
                  for(int i=0;i<rowCount;++i){
                  const QVariant currVal = model()->index(i,column,parent ).data(role );
                  if(currVal.isNull()) continue;
                  if(currVal == myValue)
                  return setCurrentIndex(i);
                  }
                  }
                  

                  yes, it seems to be the approach.
                  thanks

                  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