// A proxy style with debugging output wrapped around the base style // for debugging metrics,etc. // 1.0, 6.8.2014 #include #include #include #include #include #include #include #include #include class DebugStyle : public QProxyStyle { public: static QString debugWidget(const QWidget *widget = 0) { if (!widget) return QStringLiteral("widget=NULL"); const QString on = widget->objectName(); if (!on.isEmpty()) return on; return widget->metaObject()->className(); } static QString debugQStyleOption(const QStyleOption *option) { QString result; if (option) QDebug(&result) << "optRec=" << option->rect; else result = QStringLiteral("option=NULL"); return result; } DebugStyle(QStyle *style) : QProxyStyle(style) { qDebug() << __FUNCTION__ << style->objectName() << "devicePixelRatio=" << qApp->devicePixelRatio(); } void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const { qDebug() << __FUNCTION__ << "element="<< element << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget); QProxyStyle::drawPrimitive( element, option, painter, widget); } void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget = 0) const { qDebug() << __FUNCTION__ << "element="<< element << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget); QProxyStyle::drawControl(element, option, painter, widget); } void drawComplexControl(ComplexControl control, const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget = 0) const { qDebug() << __FUNCTION__ << "control="<< control << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget); QProxyStyle::drawComplexControl(control, option, painter, widget); } void drawItemPixmap(QPainter *painter, const QRect &rect, int alignment, const QPixmap &pixmap) const { qDebug() << __FUNCTION__ << rect << "alignment=" << alignment << pixmap; QProxyStyle::drawItemPixmap(painter, rect, alignment, pixmap); } QSize sizeFromContents(ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const { QSize r= QProxyStyle::sizeFromContents(type, option, size, widget); qDebug() << __FUNCTION__ << size << "type=" << type << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r; return r; } QRect subElementRect(SubElement element, const QStyleOption *option, const QWidget *widget) const { QRect r = QProxyStyle::subElementRect(element, option, widget); qDebug() << __FUNCTION__ << "element=" << element << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r; return r; } QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const { QRect r = QProxyStyle::subControlRect(cc, opt, sc, widget); qDebug() << __FUNCTION__ << "cc=" << cc << "sc=" << sc << DebugStyle::debugQStyleOption(opt) << DebugStyle::debugWidget(widget) << "returns" << r; return r; } QRect itemTextRect(const QFontMetrics &fm, const QRect &r, int flags, bool enabled, const QString &text) const { QRect re = QProxyStyle::itemTextRect(fm, r, flags, enabled, text); qDebug() << __FUNCTION__ << r << "flags=" << flags << "enabled=" << enabled << text << "returns " << re; return re; } QRect itemPixmapRect(const QRect &r, int flags, const QPixmap &pixmap) const { QRect re = QProxyStyle::itemPixmapRect(r, flags, pixmap); qDebug() << __FUNCTION__ << r << "flags=" << flags << pixmap << "returns " << re; return re; } int pixelMetric(PixelMetric metric, const QStyleOption *option = 0, const QWidget *widget = 0) const { int r = QProxyStyle::pixelMetric(metric, option, widget); qDebug() << __FUNCTION__ << "metric="<< metric << DebugStyle::debugQStyleOption(option) << DebugStyle::debugWidget(widget) << "returns" << r; return r; } QPixmap standardPixmap(StandardPixmap standardPixmap, const QStyleOption *opt, const QWidget *widget = 0) const { QPixmap r = QProxyStyle::standardPixmap(standardPixmap, opt, widget); qDebug() << __FUNCTION__ << "standardPixmap=" << standardPixmap << "returns " << r; return r; } QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const { QPixmap r = QProxyStyle::generatedIconPixmap(iconMode, pixmap, opt); qDebug() << __FUNCTION__ << "iconMode=" << iconMode << pixmap << "returns " << r; return r; } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); const QString stName = QApplication::style()->objectName(); app.setStyle(new DebugStyle(QApplication::style())); ....