在应用程序窗口顶部绘制覆盖图 [英] Drawing an overlay on top of an application's window

查看:115
本文介绍了在应用程序窗口顶部绘制覆盖图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在应用程序窗口的顶部绘制,以便我可以用一些额外的诊断信息来注释所有小部件,类似于Firefox中的CSS开发人员工具(例如,添加小部件类,样式,突出显示边框等)

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 tools in Firefox (eg add widget classes, styles, highlight borders etc).

我可以遍历小部件树并提取相关信息,但是问题是如何用此信息覆盖所有应用程序窗口?

I can walk the widget tree and extract the relevant information, but the question is how can I overlay all the application windows with this information?

一种方法是覆盖我的QMainWindow的绘画事件,但这必须在所有顶级窗口中完成.是否有其他方法可以在QDesktopWidget上进行绘制?还是每个QWidget的绘制方法都有钩子?涉及子类化QWidget本身的所有内容都无法与标准小部件一起使用.

One way would be to override my QMainWindow's paint event, but this has to be done for all top level windows. Is there an alternative method where you can paint on the QDesktopWidget for instance? Or any hooks into each QWidget's paint method? Anything that involves subclassing QWidget itself won't work with the standard widgets.

这是我先前提出的问题的延续:

This follows on from my previous question:

欢呼 山d

多亏了德米特里(Dmitry),我现在有了一个非常简单且易于扩展的方法:

Thanks to Dmitry I've now got a really simple method that is easily extensible:

class DiagnosticStyle : public QWindowsVistaStyle
{
Q_OBJECT

public: 
    typedef QWindowsVistaStyle BaseStyle;
    void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const;
};


void DiagnosticStyle::drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const
{
    BaseStyle::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()); 
    }
}

qApp->setStyle(new DiagnosticStyle());

推荐答案

您可以基于QMotifStyle或其他类创建自己的样式类,并在与他的信息有关的任何小部件/控件上进行绘制.

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;

    }
}

这篇关于在应用程序窗口顶部绘制覆盖图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆