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:
{
Q_OBJECT
public:
{
}
int pixelMetric
(PixelMetric metric,
const QStyleOption* option
= 0,
const QWidget* widget
= 0) const {
if (metric == PM_SmallIconSize)
{
return 32;
}
else
}
};
class MyStyle : public QWindowsXPStyle
{
Q_OBJECT
public:
explicit MyStyle() : QWindowsXPStyle()
{
}
int pixelMetric(PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0) const
{
if (metric == PM_SmallIconSize)
{
return 32;
}
else
return QWindowsXPStyle::pixelMetric(metric, option, widget);
}
};
To copy to clipboard, switch view to plain text mode
Main:
MyStyle* pNewStyle = new MyStyle();
app.setStyle(pNewStyle);
QApplication app(argc, argv);
MyStyle* pNewStyle = new MyStyle();
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:
class ProxyStyle
: public QStyle {
public:
explicit ProxyStyle(const QString& style);
~ProxyStyle();
// Other virtual methods declarations ...
private:
};
ProxyStyle::ProxyStyle(const QString& style)
{
}
ProxyStyle::~ProxyStyle()
{
delete pStyle;
}
{
pStyle->drawComplexControl(control, option, painter, widget);
}
{
pStyle->drawControl(element, option, painter, widget);
}
// Other virtual methods implementations are similar ...
class ProxyStyle : public QStyle
{
public:
explicit ProxyStyle(const QString& style);
~ProxyStyle();
void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget = 0) const;
void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const;
// Other virtual methods declarations ...
private:
QStyle* pStyle;
};
ProxyStyle::ProxyStyle(const QString& style)
: QStyle(), pStyle(QStyleFactory::create(style))
{
}
ProxyStyle::~ProxyStyle()
{
delete pStyle;
}
void ProxyStyle::drawComplexControl(ComplexControl control, const QStyleOptionComplex* option, QPainter* painter, const QWidget* widget) const
{
pStyle->drawComplexControl(control, option, painter, widget);
}
void ProxyStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
pStyle->drawControl(element, option, painter, widget);
}
// Other virtual methods implementations are similar ...
To copy to clipboard, switch view to plain text mode
Custom style class:
class MyStyle : public ProxyStyle
{
Q_OBJECT
public:
explicit MyStyle(const QString& baseStyle) : ProxyStyle(baseStyle)
{
}
int pixelMetric
(PixelMetric metric,
const QStyleOption* option
= 0,
const QWidget* widget
= 0) const {
if (metric == PM_SmallIconSize)
{
return 32;
}
else
return ProxyStyle::pixelMetric(metric, option, widget);
}
};
class MyStyle : public ProxyStyle
{
Q_OBJECT
public:
explicit MyStyle(const QString& baseStyle) : ProxyStyle(baseStyle)
{
}
int pixelMetric(PixelMetric metric, const QStyleOption* option = 0, const QWidget* widget = 0) const
{
if (metric == PM_SmallIconSize)
{
return 32;
}
else
return ProxyStyle::pixelMetric(metric, option, widget);
}
};
To copy to clipboard, switch view to plain text mode
Main
MyStyle* pNewStyle = new MyStyle(app.style()->objectName());
app.setStyle(pNewStyle);
QApplication app(argc, argv);
MyStyle* pNewStyle = new MyStyle(app.style()->objectName());
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.
Bookmarks