Drawing an overlay on top of an application's window

后端 未结 2 1683
自闭症患者
自闭症患者 2020-12-31 05:19

I want to be able to paint on top of my application\'s window so that I can annotate all the widgets with some extra diagnostic information, similar to the CSS developer too

2条回答
  •  温柔的废话
    2020-12-31 05:58

    Somewhere in Qt5 the styles (GTK, Windows, etc) were made internal. Now you need to use QCommonStyle.

    If anyone's wondering how to do this with Qt5+. Here's a self-contained version of @the_mandrill's code above.

    class DiagnosticStyle : public QCommonStyle
    {
    Q_OBJECT
    
    public: 
        typedef QStyle BaseStyle;
        void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
        {
            QCommonStyle::drawControl(element, option, painter, widget);
            if (widget && painter) {
                // draw a border around the widget
                painter->setPen(QColor("red"));
                painter->drawRect(widget->rect());
    
                // show the classname of the widget
                QBrush translucentBrush(QColor(255,246,240, 100));
                painter->fillRect(widget->rect(), translucentBrush);
                painter->setPen(QColor("darkblue"));
                painter->drawText(widget->rect(), Qt::AlignLeft | Qt::AlignVCenter, widget->metaObject()->className()); 
            }
        };
    };
    

    Then, in your main window constructor call

    qApp->setStyle(new DiagnosticStyle());
    

提交回复
热议问题