Results 1 to 4 of 4

Thread: "Proxy Style" does not work!

  1. #1
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question "Proxy Style" does not work!

    Hi everybody!

    I'm trying to use a "Proxy style" technique in order to change menu icon size and faced a problem. When I use a direct inheritance from the platform-specific style, all is OK:
    Qt Code:
    1. class MyStyle : public QWindowsXPStyle
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyStyle() : QWindowsXPStyle()
    7. {
    8. }
    9. int pixelMetric(PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0) const
    10. {
    11. if (metric == PM_SmallIconSize)
    12. {
    13. return 32;
    14. }
    15. else
    16. return QWindowsXPStyle::pixelMetric(metric, option, widget);
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Main:
    Qt Code:
    1. QApplication app(argc, argv);
    2. MyStyle* pNewStyle = new MyStyle();
    3. app.setStyle(pNewStyle);
    To copy to clipboard, switch view to plain text mode 

    The code above is working properly, changing the menu icon size to 32x32. Then I tried to use "Proxy style", but it does not work. Here is the implementaion.
    Proxy class:
    Qt Code:
    1. class ProxyStyle : public QStyle
    2. {
    3. public:
    4. explicit ProxyStyle(const QString& style);
    5. ~ProxyStyle();
    6. void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget = 0) const;
    7. void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const;
    8.  
    9. // Other virtual methods declarations ...
    10.  
    11. private:
    12. QStyle* pStyle;
    13. };
    14.  
    15. ProxyStyle::ProxyStyle(const QString& style)
    16. : QStyle(), pStyle(QStyleFactory::create(style))
    17. {
    18. }
    19. ProxyStyle::~ProxyStyle()
    20. {
    21. delete pStyle;
    22. }
    23. void ProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const
    24. {
    25. pStyle->drawComplexControl(control, option, painter, widget);
    26. }
    27.  
    28. void ProxyStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
    29. {
    30. pStyle->drawControl(element, option, painter, widget);
    31. }
    32.  
    33. // Other virtual methods implementations are similar ...
    To copy to clipboard, switch view to plain text mode 

    Custom style class:
    Qt Code:
    1. class MyStyle : public ProxyStyle
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MyStyle(const QString& baseStyle) : ProxyStyle(baseStyle)
    7. {
    8. }
    9. int pixelMetric(PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0) const
    10. {
    11. if (metric == PM_SmallIconSize)
    12. {
    13. return 32;
    14. }
    15. else
    16. return ProxyStyle::pixelMetric(metric, option, widget);
    17. }
    18. };
    To copy to clipboard, switch view to plain text mode 
    Main
    Qt Code:
    1. QApplication app(argc, argv);
    2. MyStyle* pNewStyle = new MyStyle(app.style()->objectName());
    3. app.setStyle(pNewStyle);
    To copy to clipboard, switch view to plain text mode 

    I noticed in debugger, that MyStyle::pixelMetric() method is called before the first appearance of each menu, returned the proper size (32), but icons in menu remains the same (16x16).
    Platform details: Qt 4.5.1, WinXP SP3, Visual C++ 2005 SP1.

    Can anybody help me?
    Thanks in advance.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: "Proxy Style" does not work!

    this is limitation of the proxy pattern:
    once the proxy has delegated (aka called) something to the "proxied" style, that style will call only its own methods.
    I.e. overriding a virtual method does not help, as there are two classes involved - each with its own virtual method lookup table. Once you have "stepped into the realm" of the proxied class, there is no way back as that class does not know about the proxy and as you have not subclassed the real style, virtual methods don't help.

    If you find a way about that problem, I'd be interested, too.
    I doubt there is one, though. (Maybe with templates, if it is ok that the proxy works only with a set of styles defined at compile time.)

    HTH

  3. #3
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Thanks!

    Thank you, I have missed this detail! Tracing into Qt src I noticed, that PixelMetric() called
    from DrawControl(). Of cause, after DrawControl() was delegated to the "proxied" style, it
    can call only PixelMetric() implementation of this style (not my overriden PixelMetric()
    implementation). In My case I should override DrawControl(), that is too complicated. I
    don't see elegant solution at this time, and I'll return to use a direct inheritance
    from the platform-specific styles.

    Thank you for participation.

  4. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Thanks!


Similar Threads

  1. getting MySQL to work with Qt
    By Ashish in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2009, 08:57
  2. Qt4 : QPainter::setRedirected doesn't work
    By Ankitha Varsha in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2008, 17:52
  3. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  4. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 07:20

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.