QGraphicsProxyWidget 与 QWidget 点滴

QGraphicsProxyWidget 与 QWidget 点滴

标题不好取,起源于CSDN中看到有网友提问:如果将一个QWidget同时显示在 QGraphicsView 和其他和view同级的普通的Widget中。

QGraphicsProxyWidget

QGraphicsProxyWidget 是为将 QWidget 嵌入到 QGraphicsScene 中而引入的代理。

  • 将 event 在二者之间进行传递
  • 基于整数的 QWidget 的坐标和基于浮点数的 QGraphicsScene 坐标间的变换
QWidget 是如何嵌入的?

我们知道一个 QWidget 只能在一个地方出现,而同一个 scene 却可以在多个 view 中出现。这是怎么做的呢?多个 view 中出现的会是同一个 QWidget 么?

嵌入 QWidget ,可以先构建 QGraphicsProxyWidget,对其 setWidget(),然后添加到 scene 中;也可以直接调用 QGraphicsScene的成员 addWidget()

这个过程中对 QWidget 做了哪些动作呢?

可以看看源码:qgraphicsproxywidget.cpp

void QGraphicsProxyWidgetPrivate::setWidget_helper(QWidget *newWidget, bool autoShow)

我们只看部分代码片段。

确保:被嵌入的是 顶级QWidget,或者是被嵌入QWidget 对象的子对象

if (!newWidget->isWindow()) {
QWExtra *extra = newWidget->parentWidget()->d_func()->extra;
if (!extra || !extra->proxyWidget) {
qWarning("QGraphicsProxyWidget::setWidget: cannot embed widget %p "
"which is not a toplevel widget, and is not a child of an embedded widget", newWidget);
return;
}
}

将该 QGraphicsProxyWidget 注册到 QWidget 的 QWidgetPrivate 成员中

// Register this proxy within the widget's private.
// ### This is a bit backdoorish
QWExtra *extra = newWidget->d_func()->extra;
if (!extra) {
newWidget->d_func()->createExtra();
extra = newWidget->d_func()->extra;
}
QGraphicsProxyWidget **proxyWidget = &extra->proxyWidget;
if (*proxyWidget) {
if (*proxyWidget != q) {
qWarning("QGraphicsProxyWidget::setWidget: cannot embed widget %p"
"; already embedded", newWidget);
}
return;
}
*proxyWidget = q;

设置属性,使其不再屏幕上显示,并且不会在 close 是被自动删除

newWidget->setAttribute(Qt::WA_DontShowOnScreen);
newWidget->ensurePolished();
// Do not wait for this widget to close before the app closes ###
// shouldn't this widget inherit the attribute?
newWidget->setAttribute(Qt::WA_QuitOnClose, false);
q->setAcceptHoverEvents(true);

安装事件过滤器

// Hook up the event filter to keep the state up to date.
newWidget->installEventFilter(q);
QObject::connect(newWidget, SIGNAL(destroyed()), q, SLOT(_q_removeWidgetSlot()));paintpaint 与 paintEvent

QGraphicsProxyWidget 是 QGraphicsItem 的派生类,可知其显示的内容也是在 paint 函数中进行绘制的。

去掉无关紧要的部分,可以看出,此处调用的是 QWidget 的 render 成员,进而调用 QWidget 的 paintEvent 成员

void QGraphicsProxyWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_D(QGraphicsProxyWidget);
const QRect exposedWidgetRect = (option->exposedRect & rect()).toAlignedRect();
d->widget->render(painter, exposedWidgetRect.topLeft(), exposedWidgetRect);
}

看一下从 QWidget::render 到 QWidget::paintEvent 的调用关系

QWidget::render()
QWidgetPrivate::render()

QWidgetPrivate::drawWidget()

//事件系统
QCoreApplication::sendSpontaneousEvent()
QCoreApplication::notifyInternal()
QApplication::notify()
QApplicationPrivate::notify_helper()
QWidget::event()

QWidget::paintEvent()repaint 与 update

顺便看一下从 QWidget::repaint 到 QWidgetPrivate::drawWidget

QWidget::repaint()
QWidgetBackingStore::markDirty()

//事件系统
QCoreApplication::sendEvent()
QCoreApplication::notifyInternal()
QApplication::notify()
QApplicationPrivate::notify_helper()
QWidget::event()

QWidgetPrivate::syncBackingStore()
QWidgetBackingStore::sync()
QWidgetPrivate::drawWidget()
...

以及 QWidget::update 与 事件的发送

QWidget::update()
QWidgetBackingStore::markDirty()
QCoreApplication::postEvent()双缓冲?

QWidget 的实现采用的双缓冲方式,上面提到的 QWidgetBAckingStore 类应该与此有关了。

可以看一下 QWidgetBackingStore::sync() 的部分代码(和 QGraphicsProxyWidget 有关部分)

for (int i = 0; i < dirtyWidgets.size(); ++i) {
...
#ifndef QT_NO_GRAPHICSVIEW
if (tlw->d_func()->extra->proxyWidget) {
resetWidget(w);
continue;
}
#endif
...
}
...
#ifndef QT_NO_GRAPHICSVIEW
if (tlw->d_func()->extra->proxyWidget) {
updateStaticContentsSize();
dirty = QRegion();
const QVector<QRect> rects(toClean.rects());
for (int i = 0; i < rects.size(); ++i)
tlw->d_func()->extra->proxyWidget->update(rects.at(i));
return;
}
#endif

从这儿可以猜到,一旦为一个QWidget对应QWidgetPrivate 设置了 proxyWidget,其自身的绘制行为将会很很大的不同


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值