QGraphicsProxyWidget paintEvent(from 1+1 =2)


标题不好取,起源于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,其自身的绘制行为将会很很大的不同

优质内容筛选与推荐>>
1、TYVJ 1061 Mobile Service 解题报告
2、Aspectj---- call execution
3、Flex 与 Asp.Net 通过 Remoting 方式进行通讯
4、电脑快捷键
5、入园第一天


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn