我希望能够在我的应用程序窗口上画画,这样我就可以用一些额外的诊断信息来注释所有的小部件,类似于Firefox中的CSS开发工具(例如添加小部件类,样式,突出显示边框等)。
我可以遍历小部件树并提取相关信息,但问题是如何用这些信息覆盖所有应用程序窗口?
一种方法是重写我的QMainWindow
的paint事件,但这必须对所有顶层窗口完成。有没有一种替代的方法,你可以画在QDesktopWidget
上,例如?或任何挂钩到每个QWidget
的油漆方法?任何涉及子类化QWidget
本身的东西都不能与标准小部件一起工作。
这是我上一个问题的延续:
- 有什么有用的工具来诊断Qt布局和间距问题?
干杯山魈
编辑:多亏了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());
你可以创建自己的基于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;
}
}
在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());