日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

QGraphicsProxyWidget paintEvent(from 1+1 =2)

發(fā)布時間:2023/12/10 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QGraphicsProxyWidget paintEvent(from 1+1 =2) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

標題不好取,起源于CSDN中看到有網(wǎng)友提問:如果將一個QWidget同時顯示在 QGraphicsView 和其他和view同級的普通的Widget中。

QGraphicsProxyWidget

QGraphicsProxyWidget 是為將 QWidget 嵌入到 QGraphicsScene 中而引入的代理。

  • 將 event 在二者之間進行傳遞
  • 基于整數(shù)的 QWidget 的坐標和基于浮點數(shù)的 QGraphicsScene 坐標間的變換
QWidget 是如何嵌入的?

我們知道一個 QWidget 只能在一個地方出現(xiàn),而同一個 scene 卻可以在多個 view 中出現(xiàn)。這是怎么做的呢?多個 view 中出現(xiàn)的會是同一個 QWidget 么?

嵌入 QWidget ,可以先構(gòu)建 QGraphicsProxyWidget,對其 setWidget(),然后添加到 scene 中;也可以直接調(diào)用 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;

設(shè)置屬性,使其不再屏幕上顯示,并且不會在 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 的派生類,可知其顯示的內(nèi)容也是在 paint 函數(shù)中進行繪制的。

去掉無關(guān)緊要的部分,可以看出,此處調(diào)用的是 QWidget 的 render 成員,進而調(diào)用 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 的調(diào)用關(guān)系

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

QWidgetPrivate::drawWidget()

//事件系統(tǒng)
QCoreApplication::sendSpontaneousEvent()
QCoreApplication::notifyInternal()
QApplication::notify()
QApplicationPrivate::notify_helper()
QWidget::event()

QWidget::paintEvent()repaint 與 update

順便看一下從 QWidget::repaint 到 QWidgetPrivate::drawWidget

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

//事件系統(tǒng)
QCoreApplication::sendEvent()
QCoreApplication::notifyInternal()
QApplication::notify()
QApplicationPrivate::notify_helper()
QWidget::event()

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

以及 QWidget::update 與 事件的發(fā)送

QWidget::update()
QWidgetBackingStore::markDirty()
QCoreApplication::postEvent()雙緩沖?

QWidget 的實現(xiàn)采用的雙緩沖方式,上面提到的 QWidgetBAckingStore 類應(yīng)該與此有關(guān)了。

可以看一下 QWidgetBackingStore::sync() 的部分代碼(和 QGraphicsProxyWidget 有關(guān)部分)

?

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對應(yīng)QWidgetPrivate 設(shè)置了 proxyWidget,其自身的繪制行為將會很很大的不同

轉(zhuǎn)載于:https://www.cnblogs.com/zhaifd/archive/2013/06/03/3116154.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。