OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Set checkbox indicator width or bg color within custom QStyledItemDelegate.paint()

  • Thread starter Thread starter bernardlebel
  • Start date Start date
B

bernardlebel

Guest
I have a QTableView, and a custom QStyledItemDelegate for some of the columns so that they display a centered checkbox. It works, but the checkbox's indicator (the border, basically) is thin and hard to see against the background:

enter image description here

I'd like to make the indicator thicker. If that's not possible, setting the background color so that it stands out a bit more from the background would be better than nothing.

I'm a bit lost in the delegate/qstyle/qpalette/qrect meander. Any help would be appreciated.

Here's my code:

Code:
    def paint(self,
              painter: QtGui.QPainter,
              option: QtWidgets.QStyleOptionViewItem,
              proxy_index: QtCore.QModelIndex):
        self.initStyleOption(option, proxy_index)

        option = QtWidgets.QStyleOptionViewItem(option)
        widget = option.widget
        style = widget.style() if widget else QtWidgets.QApplication.style()
        style.drawPrimitive(
            style.PE_PanelItemViewItem,
            option,
            painter,
            option.widget
        )

        rect = style.subElementRect(
            QtWidgets.QStyle.SE_CheckBoxIndicator,
            option,
            widget=option.widget
        )

        option.rect = style.alignedRect(
            option.direction,
            Qt.AlignCenter,
            rect.size(),
            option.rect
        )

        proxy_model = proxy_index.model()
        source_model = proxy_model.sourceModel()
        source_index = proxy_model.mapToSource(proxy_index)

        item = source_model.itemFromIndex(source_index)
        option.state = style.State_On if item.checked else style.State_Off

        style.drawPrimitive(
            style.PE_IndicatorItemViewItemCheck,
            option,
            painter,
            widget=option.widget
        )

I looked through all the abovementioned docs but nothing specifically stood out as the solution. I also set the QTableView's style sheet to QCheckbox::indicator {border: 1px solid <some color>;} but that didn't help.
<p>I have a QTableView, and a custom QStyledItemDelegate for some of the columns so that they display a centered checkbox. It works, but the checkbox's indicator (the border, basically) is thin and hard to see against the background:</p>
<p><a href="https://i.sstatic.net/0YdAmhCY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0YdAmhCY.png" alt="enter image description here" /></a></p>
<p>I'd like to make the indicator thicker. If that's not possible, setting the background color so that it stands out a bit more from the background would be better than nothing.</p>
<p>I'm a bit lost in the delegate/qstyle/qpalette/qrect meander. Any help would be appreciated.</p>
<p>Here's my code:</p>
<pre class="lang-py prettyprint-override"><code> def paint(self,
painter: QtGui.QPainter,
option: QtWidgets.QStyleOptionViewItem,
proxy_index: QtCore.QModelIndex):
self.initStyleOption(option, proxy_index)

option = QtWidgets.QStyleOptionViewItem(option)
widget = option.widget
style = widget.style() if widget else QtWidgets.QApplication.style()
style.drawPrimitive(
style.PE_PanelItemViewItem,
option,
painter,
option.widget
)

rect = style.subElementRect(
QtWidgets.QStyle.SE_CheckBoxIndicator,
option,
widget=option.widget
)

option.rect = style.alignedRect(
option.direction,
Qt.AlignCenter,
rect.size(),
option.rect
)

proxy_model = proxy_index.model()
source_model = proxy_model.sourceModel()
source_index = proxy_model.mapToSource(proxy_index)

item = source_model.itemFromIndex(source_index)
option.state = style.State_On if item.checked else style.State_Off

style.drawPrimitive(
style.PE_IndicatorItemViewItemCheck,
option,
painter,
widget=option.widget
)
</code></pre>
<p>I looked through all the abovementioned docs but nothing specifically stood out as the solution. I also set the QTableView's style sheet to <code>QCheckbox::indicator {border: 1px solid <some color>;}</code> but that didn't help.</p>
 

Latest posts

Top