在应用程序窗口顶部绘制覆盖图

问题描述 投票:13回答:3

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

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

一种方法是重写我的QMainWindow的绘画事件,但这必须在所有顶级窗口中完成。是否有其他方法可以在QDesktopWidget上绘画?还是每个QWidget的绘制方法都有钩子?涉及子类化QWidget本身的所有内容均不适用于标准小部件。

这是我之前的问题:

欢呼山d

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

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());
qt paint
3个回答
6
投票

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

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;

    }
}

2
投票

此Wiki解释了有关在QWidget上绘制内容的信息。

http://developer.nokia.com/community/wiki/Archived:How_to_overlay_QWidget_on_top_of_another

您可能想做这样的事情。


0
投票

在Qt5的某个地方,样式(GTK,Windows等)是内部样式。现在您需要使用QCommonStyle。

[如果有人想知道如何使用Qt5 +来做到这一点。这是上面@the_mandrill的代码的独立版本。

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()); 
        }
    };
};

然后,在您的主窗口构造函数调用中

qApp->setStyle(new DiagnosticStyle());
© www.soinside.com 2019 - 2024. All rights reserved.