Get checkbox of QStandardItem
I have a custom QComboBox using a model in which I set some QStandardItems. I made these items checkable:
Code:
item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
The problem is when I deploy my app for Android, the checkboxes are so small I cannot even click on them. That's why I would like to set a style for them, so my question is: how do I get the checkbox objects of a QStandardItem so I can call QWidget::setStyle() on them?
Additional issue to be taken into account: the checkboxes do not always show up depending on the OS, so I'm using one of these workarounds:
Re: Get checkbox of QStandardItem
There are no checkbox objects. The boxes are drawn by a delegate, so the easiest approach is to provide a custom delegate that will draw the boxes the way you want.
Re: Get checkbox of QStandardItem
Thanks for your answer.
Ok then how do I find which image file Qt was using before, so I can paint it again in a bigger size? Looking into the source of QStyledItemDelegate did not provide any information about that.
How can I paint an image in a delegate? The QStyledItemDelegate doc does not seem to cover painting images.
And most importantly, how do I tell Qt that the painted image is the actual checkbox (so when I click it the item changes the value of its Qt::CheckStateRole)?
Re: Get checkbox of QStandardItem
Have a look at QStylePainter. Regarding the second question, the delegate handles events for items in editorEvent() method, you have to implement a hit test there and update the model if you hit the checkbox.
Re: Get checkbox of QStandardItem
Thanks. I'm playing around with QStylePainter right now. I'll let you know if I get to achieve what I want.
Re: Get checkbox of QStandardItem
What you want is probably drawControl() and QStyle::CE_CheckBox.
Re: Get checkbox of QStandardItem
So how to use these?
I'm not sure but I suspect this will paint the default icons in their default size, so much too small in the case of Android. Or are you thinking of a way to resize them?
Anyway I'm experimenting with the advice in your previous post now and I feel like it will do fine. :)
Re: Get checkbox of QStandardItem
I managed to get by with the paint() method, but I'm kinda stuck with editorEvent(). Edit: I had a look at QItemDelegate source and found how to do it:
Code:
{
Q_UNUSED(option);
if (event
->type
() == QEvent::MouseButtonRelease) { QVariant value
= index.
data(Qt
::CheckStateRole);
Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
? Qt::Unchecked : Qt::Checked);
return model->setData(index, state, Qt::CheckStateRole);
} else
return false;
}
This will intercept all mouse clicks on the item (and I'm ok with it).
Re: Get checkbox of QStandardItem
Update:
I think I nailed it, hinted by this post.
So I subclassed QProxyStyle as AndroidStyle and reimplemented QProxyStyle::pixelMetric this way:
Code:
int AndroidStyle::pixelMetric(PixelMetric which,
{
int metric = QProxyStyle::pixelMetric(which, option, widget);
switch (which) {
case PM_IndicatorWidth:
case PM_IndicatorHeight:
return 2*metric;
default:
return metric;
}
}
and I set this style in the main function like this:
Code:
a.setStyle(new AndroidStyle);