How to change QStyle properties in PyQt4?

I would like to change the QStyle::PM_TabBarTabHSpace for the PyQt application. I read the Qt document for QStyle , but I'm not sure how to install it correctly in PyQt.

Inoperative code:

 style = QStyleFactory.create('Cleanlooks') style.PM_TabBarTabHSpace = 5 # 5 pixels? app.setStyle(style) 

This code works, but it does not change the indentation on the tab tabs. I tried to use stylesheets to change the filling of the tab, but this destroys the graphic drawing, so that none of the elements of the search style are drawn by default (I do not want to override all the ui drawings).

I think I may have to use QProxyStyle , but I cannot find examples of using this in PyQt4. Edit: PyQt doesn't seem to have a QProxyStyle, since from PyQt4.QtGui import QProxyStyle fails.

Can someone please write an example of changing the value of PM_TabBarTabHSpace ? Thanks.

Edit Here is the skeleton code. Changing the PM_TabBarTabHSpace value does nothing. :(

 from PyQt4.QtGui import (QApplication, QTabWidget, QWidget, QStyle, QStyleFactory) def myPixelMetric(self, option=None, widget=None): if option == QStyle.PM_TabBarTabHSpace: return 200 # pixels else: return QStyle.pixelMetric(option, widget) style = QStyleFactory.create('Windows') style.pixelMetric = myPixelMetric app = QApplication('test -style Cleanlooks'.split()) # Override style app.setStyle(style) tab = QTabWidget() tab.addTab(QWidget(), 'one') tab.addTab(QWidget(), 'two') tab.show() app.exec_() 
+4
source share
1 answer

QStyle.pixelMetric (...) is a built-in class method. You cannot set using the pointing function. Because it's code C. You can test it with the addition of

 def myPixelMetric(self, option=None, widget=None): print 'Debug, i am calling' ... 

in your myPixelmetric function. To do this, you need to subclass the Style object. Here is an example:

 class MyStyle(QCommonStyle): def pixelMetric(self, QStyle_PixelMetric, QStyleOption_option=None, QWidget_widget=None): if QStyle_PixelMetric == QStyle.PM_TabBarTabHSpace: return 200 else: return QCommonStyle.pixelMetric(self, QStyle_PixelMetric, QStyleOption_option, QWidget_widget) app = QApplication('test -style Cleanlooks'.split()) app.setStyle(MyStyle()) 

This piece of code will work, but it is ugly. I prefer to use styles to control the style.

+4
source

Source: https://habr.com/ru/post/1341411/


All Articles