PDA

View Full Version : A reliable way to get the size of a checkbox indicator



zoeker
15th February 2008, 12:59
Hi,

A little introduction to the problem: I'm using a QTreeView where one of the columns is editable and the data is presented as checkboxes which, on the one hand, should look like the native checkboxes of the particular OS, and, on the other, should be centered in the tree column. For this, I'm using a CheckBoxDelegate class, derived from QItemDelegate.

To center the checkbox, I can either use the static QRect::alignedRect() or calculate the appropriate rectangle myself. In both cases however, a prerequisite is to know how big actually the checkbox is.

Initially I thought that calling the QStyle::pixelMetric() function with QStyle::PM_IndicatorWidth and QStyle::PM_IndicatorHeight would give me the size of a normal checkbox. The problem is that on Linux, this function returns a width that is too narrow and the checkbox's right border is not visible, while the same code on Windows produces a perfect checkbox. I certainly don't like the thought of adding some magic number of extra pixels to the indicator width returned by pixelMetric.

I'd appreciate any thoughts and/or suggestions.

jpn
15th February 2008, 21:05
Perhaps you could use built-in checkable items and get them center aligned by using a proxy style and reimplementing QStyle::subElementRect(SE_ViewItemCheckIndicator)?