Boolean values and Qt roles in models
-
wrote on 17 May 2021, 09:42 last edited by
Hi,
as far as i understand it, if the role Qt::CheckStateRole is not used in the data method of a QAbstractTableModel a boolean value would be displayed as string (true/false). With the additional Qt::CheckStateRole i managed to get a default CheckBox alongside with string representation of the boolean value. The corresponding column is set to be editable and checkable. So now i can alter the string representation but the CheckBox does not respond to user input, although the Qt::ItemIsUserCheckable is set in the flags method too. Are those flags mutually exclusive in that way?
-
Do you properly handle Qt::CheckStateRole in your setData() function?
-
wrote on 17 May 2021, 10:19 last edited by
Also,
Qt::CheckStateRole
does not expect a boolean but a Qt::CheckState -
Do you properly handle Qt::CheckStateRole in your setData() function?
wrote on 17 May 2021, 10:32 last edited by VRonin} else if (role==Qt::CheckStateRole) { int zeile, spalte; zeile = index.row(); spalte = index.column(); nlohmann::json row_obj = ZTableModel::get_row_obj(zeile); QList<QString> headerliste = ZTableModel::get_header(j_zl); for (int i=0; i<headerliste.size(); ++i) { if (i == spalte) { QString qs = headerliste.at(i); std::string spaltenname = qs.toStdString(); QVariant qvtest = value; if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool))) { bool neuerboolwert = value.toBool(); row_obj[spaltenname] = neuerboolwert; } } } emit dataChanged(index, index); return true;
This is the part of the setData() function, that should handle Qt::CheckStateRole. If it is inappropriate for boolean values, i wonder which role should be used instead.
-
@andi456 said in Boolean values and Qt roles in models:
i wonder which role should be used instead.
Read the post from @VRonin or the documentation: "This role is used to obtain the checked state of an item. (Qt::CheckState)"
-
} else if (role==Qt::CheckStateRole) { int zeile, spalte; zeile = index.row(); spalte = index.column(); nlohmann::json row_obj = ZTableModel::get_row_obj(zeile); QList<QString> headerliste = ZTableModel::get_header(j_zl); for (int i=0; i<headerliste.size(); ++i) { if (i == spalte) { QString qs = headerliste.at(i); std::string spaltenname = qs.toStdString(); QVariant qvtest = value; if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool))) { bool neuerboolwert = value.toBool(); row_obj[spaltenname] = neuerboolwert; } } } emit dataChanged(index, index); return true;
This is the part of the setData() function, that should handle Qt::CheckStateRole. If it is inappropriate for boolean values, i wonder which role should be used instead.
wrote on 17 May 2021, 10:53 last edited by@andi456 said in Boolean values and Qt roles in models:
for (int i=0; i<headerliste.size(); ++i) {
if (i == spalte)Why are you iterating?
if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))
As mentioned
Qt::CheckStateRole
does not take a bool. When you click on the checkboxvalue
will containQt::Checked
that equals 2. -
@andi456 said in Boolean values and Qt roles in models:
for (int i=0; i<headerliste.size(); ++i) {
if (i == spalte)Why are you iterating?
if ((value.canConvert<bool>()) && (qvtest.convert(QVariant::Bool)))
As mentioned
Qt::CheckStateRole
does not take a bool. When you click on the checkboxvalue
will containQt::Checked
that equals 2. -
wrote on 17 May 2021, 11:57 last edited by andi456
int zeile, spalte; zeile = index.row(); spalte = index.column(); nlohmann::json row_obj = ZTableModel::get_row_obj(zeile); QList<QString> headerliste = ZTableModel::get_header(j_zl); QString qs = headerliste.at(spalte); std::string spaltenname = qs.toStdString(); QVariant qvtest = value; bool neuerboolwert; if ((value.canConvert<int>()) && (qvtest.convert(QVariant::Int))) { int checker = value.toInt(); if (checker==Qt::Checked) { neuerboolwert = true; std::cout << "Checked" << std::endl; row_obj[spaltenname] = neuerboolwert; //code to update the data structure } else { neuerboolwert = false; std::cout << "UnChecked" << std::endl; row_obj[spaltenname] = neuerboolwert; //code to update the data structure } } emit dataChanged(index, index); return true;
Seems to work with the corresponding code to set Qt::Checked/Qt::Unchecked values on the data() function. Anyway, I'll disable the editable flag for boolean values, because i can still enter text to the right of checkbox, which overrides it (the checkbox).
Thanks for your support.
Kind regards,
Andreas
-
wrote on 18 May 2021, 09:01 last edited by
@andi456 said in Boolean values and Qt roles in models:
i can still enter text to the right of checkbox, which overrides it (the checkbox).
If this happens it's a bug in your
setData
/data
implementation -
@VRonin said in Boolean values and Qt roles in models:
If this happens it's a bug in your setData/data implementation
And flags() - it returns Qt::ItemIsEditable
1/10