日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

QGraphicsProxyWidget paintEvent(from 1+1 =2)

發布時間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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,其自身的繪制行為將會很很大的不同

轉載于:https://www.cnblogs.com/zhaifd/archive/2013/06/03/3116154.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的QGraphicsProxyWidget paintEvent(from 1+1 =2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。