在应用程序窗口顶部绘制覆盖层
我希望能够在我的应用程序窗口顶部进行绘制,以便我可以使用一些额外的诊断信息来注释所有小部件,类似于 Firefox 中的 CSS 开发人员工具(例如添加小部件类、样式、突出显示边框等)。
我可以遍历小部件树并提取相关信息,但问题是如何使用这些信息覆盖所有应用程序窗口?
一种方法是重写 QMainWindow 的绘制事件,但必须对所有顶级窗口执行此操作。例如,是否有其他方法可以在 QDesktopWidget
上绘画?或者每个 QWidget
的绘制方法有任何挂钩吗?任何涉及 QWidget 本身子类化的内容都不适用于标准小部件。
这是我之前的问题的延续:
干杯 山魈
编辑: 感谢 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());
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?
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:
cheers
Mandrill
EDIT:
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以基于 QMotifStyle 或其他创建自己的样式类,并在与他的信息相关的任何小部件/控件上进行绘制。
You can create own style class based on QMotifStyle or other ... and paint on any widget/control related to him information.
在 Qt5 的某个地方,样式(GTK、Windows 等)是内部的。现在您需要使用 QCommonStyle。
如果有人想知道如何使用 Qt5+ 做到这一点。这是上面 @the_mandrill 代码的独立版本。
然后,在主窗口构造函数中调用
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.
Then, in your main window constructor call