Results 1 to 7 of 7

Thread: QAbstractItemModel::match bug?

  1. #1
    Join Date
    Feb 2017
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QAbstractItemModel::match bug?

    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 finecombo.PNG
    but when I try to set the value in combo acording with the database table, I use this code
    Qt Code:
    1. void MyComboBox::setValor(QVariant myValue)
    2. {
    3. QModelIndexList myFinded=this->model()->match(this->model()->index(0,0),Qt::DisplayRole,myValue,5,Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap));
    4.  
    5. this->setCurrentIndex(myFinded[0].row());
    6. }
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel::match bug?

    Why is 4th argument in match() 5 ?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2017
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QAbstractItemModel::match bug?

    In fact I only expect to get 1 index in myFinded (the values in the table are Uniques). But the application didn't work propertly so I have increased Hits (number of hits until macht stop searching) from 1 to 5 (number of items in my table) to debug.

    If myValue is other than 0, all work good, but when myValue=0 I get 2 indexes in myFinded

  4. #4
    Join Date
    Feb 2017
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QAbstractItemModel::match bug?

    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

    my table is table.PNG
    Qt Code:
    1. QVariant var1=QVariant(0); // variant containing the integer 0
    2.  
    3.  
    4. miConsulta->setQuery("Select Valor, Descripcion From myTable");
    5. QVariant var2=miConsulta->record(0).value(0);
    To copy to clipboard, switch view to plain text mode 
    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?????????
    can somebody explain?
    thank's

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QAbstractItemModel::match bug?

    That's the way it is
    Quote Originally Posted by Qt Documentation
    bool QVariant::isNull() const
    Returns true if this is a null variant, false otherwise. 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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    Koyi (24th February 2017)

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,010
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QAbstractItemModel::match bug?

    By the way, do you realize that this is incorrect syntax for OR-ing flags?

    Qt Code:
    1. Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap)
    To copy to clipboard, switch view to plain text mode 

    It should be this:

    Qt Code:
    1. Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap)
    To copy to clipboard, switch view to plain text mode 

    A simple typo like this could lead to all sorts of unexplained behavior, because the result of the logical OR (||) will probably not be the same as the result of the bitwise OR (|).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    Koyi (24th February 2017)

  9. #7
    Join Date
    Feb 2017
    Posts
    4
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QAbstractItemModel::match bug?

    Quote Originally Posted by d_stranz View Post
    By the way, do you realize that this is incorrect syntax for OR-ing flags?

    Qt Code:
    1. Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap)
    To copy to clipboard, switch view to plain text mode 

    It should be this:

    Qt Code:
    1. Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap)
    To copy to clipboard, switch view to plain text mode 

    A simple typo like this could lead to all sorts of unexplained behavior, because the result of the logical OR (||) will probably not be the same as the result of the bitwise OR (|).
    Ohh my God!!!! Great mistake
    but in this case, the result is the same. The problem was that operator== don't work as I espected

Similar Threads

  1. QAbstractItemModel and select folder after found match
    By matsukan in forum Qt Programming
    Replies: 2
    Last Post: 12th September 2016, 11:33
  2. no match fo 'operator[]='
    By jeff28 in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2012, 09:53
  3. No Match for Call
    By Atomic_Sheep in forum Newbie
    Replies: 6
    Last Post: 3rd May 2012, 10:16
  4. Replies: 0
    Last Post: 5th April 2012, 14:28
  5. No match for operator>>
    By Salazaar in forum Newbie
    Replies: 18
    Last Post: 12th June 2007, 17:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.