1 Attachment(s)
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 fineAttachment 12346
but when I try to set the value in combo acording with the database table, I use this code
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
Re: QAbstractItemModel::match bug?
Why is 4th argument in match() 5 ?
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
1 Attachment(s)
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 Attachment 12347
Code:
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?????????
can somebody explain?
thank's
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.
Re: QAbstractItemModel::match bug?
By the way, do you realize that this is incorrect syntax for OR-ing flags?
Code:
Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap)
It should be this:
Code:
Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap)
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 (|).
Re: QAbstractItemModel::match bug?
Quote:
Originally Posted by
d_stranz
By the way, do you realize that this is incorrect syntax for OR-ing flags?
Code:
Qt::MatchFlags(Qt::MatchExactly || Qt::MatchWrap)
It should be this:
Code:
Qt::MatchFlags(Qt::MatchExactly | Qt::MatchWrap)
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!!!!:crying::crying: Great mistake
but in this case, the result is the same. The problem was that operator== don't work as I espected