4

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_()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
k107
  • 15,882
  • 11
  • 61
  • 59
  • Doc for PyQt is here : http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qstyle.html – vettipayyan Feb 26 '11 at 13:12
  • I've read the doc, but I'm still unsure of how to set `PM_TabBarTabHSpace`. Can you provide an example? – k107 Feb 26 '11 at 22:12
  • Also, the doc only provides C examples, and no Python examples. Does this have to be done in C? – k107 Feb 26 '11 at 22:42
  • 1
    I don't know Python well, but I'm almost sure the problem is in the way you're overriding style.pixelMetric. I don't think your implementation of pixelMetric is getting called by Qt. Have you tried deriving from QStyle and providing a pixelMetric implementation in your derived class? – Stefan Monov Feb 26 '11 at 23:01
  • Also, the `style.PM_TabBarTabHSpace = 5` snippet won't work simply because QStyle doesn't have a public property "PM_TabBarTabHSpace". Your second code snippet where you try to override pixelMetric is the right approach. – Stefan Monov Feb 26 '11 at 23:03
  • I have problems deriving from QStyle because I need to implement all of the QStyle methods (which I don't know how to do properly). I would rather derive from QCleanlooksStyle or QWindowsStyle, and only override pixelMetric, but PyQt doesn't provide QCleanlooksStyle or QWindowsStyle classes. – k107 Feb 28 '11 at 19:11
  • `styleClass = QApplication.style().__class__; class MyStyle(styleClass): ...` and you can override the `pixelMetric` – Крайст Jan 30 '14 at 15:31

1 Answers1

4

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.

cengizkrbck
  • 704
  • 6
  • 21