Drawing an overlay on top of an application's window

后端 未结 2 1682
自闭症患者
自闭症患者 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:53

    You can create own style class based on QMotifStyle or other ... and paint on any widget/control related to him information.

    void MyStyle::drawPrimitive(PrimitiveElement element, const QStyleOption *option,QPainter *painter, const QWidget *widget) const
    {
         QStyle::State flags = option->state;
         QRect      rect     = option->rect;
         QPalette   pal      = option->palette;
         QBrush brush;
    
        switch (element)
        {
            case PE_FrameTabWidget:
            {
                 painter->save();
    
                     // for example: draw anything on TabWidget
                    painter->drawPixmap(rect,centerPm,centerPm.rect());
                 painter->restore();
            }
            break;
            default:
             QMotifStyle::drawPrimitive(element, option, painter, widget);
             break;
    
        }
    }
    
    0 讨论(0)
  • 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());
    
    0 讨论(0)
提交回复
热议问题