开发者

How can I change the QStyle properties in PyQt4?

开发者 https://www.devze.com 2023-02-14 02:27 出处:网络
I\'d like to change the QStyle::PM_TabBarTabHSpace property for a PyQt application.I read the Qt document for QStyle, but I\'m not sure how to set this correctly in PyQt.

I'd like to change the QStyle::PM_TabBarTabHSpace property for a PyQt application. I read the Qt document for QStyle, but I'm not sure how to set this correctly in PyQt.

Non-working code:

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

This code runs, but it doesn't change the padding on the tabbar tabs. I tried using stylesheets to change the tabbar padding, but that ruins the graphics drawing, so that none of the default look-feel stuff gets drawn (I don't want to reimplement all the ui drawing).

I think I might need to use QProxyStyle, but I can't find any examples of how to use this in PyQt4. Edit: It seems that PyQt doesn't have QProxyStyle, as开发者_如何学运维 from PyQt4.QtGui import QProxyStyle fails.

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

Edit Here is a skeleton code. Changing the PM_TabBarTabHSpace value doesn't do anything. :(

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_()


QStyle.pixelMetric(...) is built-in class method. You can not set via function pointing. Because, it is in C code. You can test it with adding

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

in your myPixelmetric function. You need to subclass Style object to achieve this. 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 code snippet will work, but it is ugly. I prefer using stylesheets over manipulating Style.

0

精彩评论

暂无评论...
验证码 换一张
取 消