So I'm trying to determine what or how can I determine particular state of StyleOption. I used the '==' operator but building a QPushbutton it seems like it returns sometimes True and sometimes False for a particular metric.
Main.py
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
from CustomStyle import MyStyles
# Subclass QMainWindow to customize your application's main window
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Widgets App")
layout = QVBoxLayout()
# button
mPushBtn = QPushButton('click')
mPushBtn.setFlat(True)
layout.addWidget(mPushBtn)
widget = QWidget()
widget.setLayout(layout)
# Set the central widget of the Window. Widget will expand
# to take up all the space in the window by default.
self.setCentralWidget(widget)
app = QApplication(sys.argv)
app.setStyle(MyStyles('QPushButton'))
window = MainWindow()
window.show()
app.exec()
CustomStyle.py
from typing import Optional, Union
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtCharts import *
class MyStyles(QCommonStyle):
def __init__(self, which_widget: str) -> None:
super().__init__(which_widget)
self.widget_name = which_widget
self.setObjectName('fpStyles')
def pixelMetric(self, m: QStyle.PixelMetric, opt: Optional[QStyleOption], widget: Optional[QWidget]) -> int:
if widget.__class__.__name__ == self.widget_name:
print(f'PixelMetric: metric > {m.name.decode()} -- styleOption > {opt.__class__.__name__} -- widget > {widget.__class__.__name__}')
if opt is not None: print(f'\tsize >>> {opt.rect.getRect()}')
if opt is not None: print(f'{opt.state == QStyle.State_Enabled}')
return super().pixelMetric(m, opt, widget)
Running the above produces following output

The QStyle
Stateenum is a flag:This means that you cannot use the equality comparison, because using
==will returnTrueonly if the state is exactlyState_Enabled.The correct operator is
&, which will mask the bits that are equal to the given value, and that can be converted to a bool to check its truthfulness:To clarify,
State_Enablecorresponds to1(both binary and integer, obviously), but if the state also contains another flag such asState_Raised(which is2, or binary10),statewill be3, or binary11.3 == 1obviously returnsFalse, but3 & 1masks the rightmost bit of3, resulting in1, andbool(1)resultsTrue.Read more about bitwise operators.
Be aware that QCommonStyle is the basis for a completely new style. You should normally use QProxyStyle.
Note: assuming your imports are done consistently, it's usually better to use
isinstance(obj, cls)instead of comparing the class name.