55 q->initStyleOption(&opt);
56 int vmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameVMargin, &opt, q),
57 hmargin = q->style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &opt, q);
58 QPoint pos(widget->x(), widget->y());
59 if (q->parentWidget() != widget->parentWidget())
60 pos = widget->parentWidget()->mapTo(q->parentWidget(), pos);
61 QRect geom(pos.x()-hmargin, pos.y()-vmargin,
62 widget->width()+(hmargin*2), widget->height()+(vmargin*2));
63 if (q->geometry() == geom)
70 if (q->style()->styleHint(QStyle::SH_FocusFrame_Mask, &opt, q, &mask))
71 q->setMask(mask.region);
123QFocusFrame::QFocusFrame(QWidget *parent)
124 : QWidget(*
new QFocusFramePrivate, parent, { })
126 setAttribute(Qt::WA_TransparentForMouseEvents);
127 setFocusPolicy(Qt::NoFocus);
128 setAttribute(Qt::WA_NoChildEventsForParent,
true);
129 setAttribute(Qt::WA_AcceptDrops, style()->styleHint(QStyle::SH_FocusFrame_AboveWidget,
nullptr,
this));
150QFocusFrame::setWidget(QWidget *widget)
154 if (style()->styleHint(QStyle::SH_FocusFrame_AboveWidget,
nullptr,
this))
155 d->showFrameAboveWidget =
true;
157 d->showFrameAboveWidget =
false;
159 if (widget == d->widget)
163 QWidget *p = d->widget;
165 p->removeEventFilter(
this);
166 if (!d->showFrameAboveWidget || p == d->frameParent)
168 p = p->parentWidget();
171 if (widget && !widget->isWindow() && widget->parentWidget()->windowType() != Qt::SubWindow) {
173 d->widget->installEventFilter(
this);
174 QWidget *p = widget->parentWidget();
175 QWidget *prev =
nullptr;
176 if (d->showFrameAboveWidget) {
183 bool isScrollArea =
false;
184 if (p->isWindow() || p->inherits(
"QToolBar") || (isScrollArea = p->inherits(
"QAbstractScrollArea"))) {
187 if (prev && isScrollArea)
188 d->frameParent = prev;
191 p->installEventFilter(
this);
193 p = p->parentWidget();
223QFocusFrame::paintEvent(QPaintEvent *)
230 QStylePainter p(
this);
232 initStyleOption(&option);
233 const int vmargin = style()->pixelMetric(QStyle::PM_FocusFrameVMargin, &option,
this);
234 const int hmargin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &option,
this);
235 QWidgetPrivate *wd = qt_widget_private(d->widget);
236 QRect rect = wd->clipRect().adjusted(0, 0, hmargin*2, vmargin*2);
238 p.drawControl(QStyle::CE_FocusFrame, option);
244QFocusFrame::eventFilter(QObject *o, QEvent *e)
247 if (o == d->widget) {
254 case QEvent::StyleChange:
257 case QEvent::ParentChange:
258 if (d->showFrameAboveWidget) {
259 QWidget *w = d->widget;
270 case QEvent::PaletteChange:
271 setPalette(d->widget->palette());
273 case QEvent::ZOrderChange:
274 if (style()->styleHint(QStyle::SH_FocusFrame_AboveWidget,
nullptr,
this))
277 stackUnder(d->widget);
279 case QEvent::Destroy:
285 }
else if (d->showFrameAboveWidget) {
292 case QEvent::ZOrderChange: