Qt源码分析--QWidget(3)--Window contents

59 篇文章 1 订阅
37 篇文章 1 订阅

1.update()

void QWidget::update()
{
    update(rect());
}

void QWidget::update(const QRect &rect)
{
    Q_D(QWidget);
    d->update(rect);
}

template <typename T>
void QWidgetPrivate::update(T r)
{
    Q_Q(QWidget);
    if (!q->isVisible() || !q->updatesEnabled())
        return;
    T clipped = r & q->rect();
    if (clipped.isEmpty())
        return;
    if (q->testAttribute(Qt::WA_WState_InPaintEvent)) {
        QCoreApplication::postEvent(q, new QUpdateLaterEvent(clipped));
        return;
    }
    QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
    if (tlwExtra && tlwExtra->backingStore)
        tlwExtra->repaintManager->markDirty(clipped, q);
}

void QWidgetRepaintManager::markDirty(const T &r, QWidget *widget, UpdateTime updateTime, BufferState bufferState)
{
    qCInfo(lcWidgetPainting) << "Marking" << r << "of" << widget << "dirty"
        << "with" << updateTime;
    Q_ASSERT(tlw->d_func()->extra);
    Q_ASSERT(tlw->d_func()->extra->topextra);
    Q_ASSERT(widget->isVisible() && widget->updatesEnabled());
    Q_ASSERT(widget->window() == tlw);
    Q_ASSERT(!r.isEmpty());
#if QT_CONFIG(graphicseffect)
    widget->d_func()->invalidateGraphicsEffectsRecursively();
#endif
    QRect widgetRect = widgetRectFor(widget, r);
    // ---------------------------------------------------------------------------
    if (widget->d_func()->shouldPaintOnScreen()) {
        if (widget->d_func()->dirty.isEmpty()) {
            widget->d_func()->dirty = r;
            sendUpdateRequest(widget, updateTime);
            return;
        } else if (qt_region_strictContains(widget->d_func()->dirty, widgetRect)) {
            if (updateTime == UpdateNow)
                sendUpdateRequest(widget, updateTime);
            return; // Already dirty
        }
        const bool eventAlreadyPosted = !widget->d_func()->dirty.isEmpty();
        widget->d_func()->dirty += r;
        if (!eventAlreadyPosted || updateTime == UpdateNow)
            sendUpdateRequest(widget, updateTime);
        return;
    }
    // ---------------------------------------------------------------------------
    if (QWidgetPrivate::get(widget)->renderToTexture) {
        if (!widget->d_func()->inDirtyList)
            addDirtyRenderToTextureWidget(widget);
        if (!updateRequestSent || updateTime == UpdateNow)
            sendUpdateRequest(tlw, updateTime);
        return;
    }
    // ---------------------------------------------------------------------------
    QRect effectiveWidgetRect = widget->d_func()->effectiveRectFor(widgetRect);
    const QPoint offset = widget->mapTo(tlw, QPoint());
    QRect translatedRect = effectiveWidgetRect.translated(offset);
#if QT_CONFIG(graphicseffect)
    // Graphics effects may exceed window size, clamp
    translatedRect = translatedRect.intersected(QRect(QPoint(), tlw->size()));
#endif
    if (qt_region_strictContains(dirty, translatedRect)) {
        if (updateTime == UpdateNow)
            sendUpdateRequest(tlw, updateTime);
        return; // Already dirty
    }
    // ---------------------------------------------------------------------------
    if (bufferState == BufferInvalid) {
        const bool eventAlreadyPosted = !dirty.isEmpty() || updateRequestSent;
#if QT_CONFIG(graphicseffect)
        if (widget->d_func()->graphicsEffect)
            dirty += widget->d_func()->effectiveRectFor(r).translated(offset);
        else
#endif
            dirty += r.translated(offset);
        if (!eventAlreadyPosted || updateTime == UpdateNow)
            sendUpdateRequest(tlw, updateTime);
        return;
    }
    // ---------------------------------------------------------------------------
    if (dirtyWidgets.isEmpty()) {
        addDirtyWidget(widget, r);
        sendUpdateRequest(tlw, updateTime);
        return;
    }
    // ---------------------------------------------------------------------------
    if (widget->d_func()->inDirtyList) {
        if (!qt_region_strictContains(widget->d_func()->dirty, effectiveWidgetRect)) {
#if QT_CONFIG(graphicseffect)
            if (widget->d_func()->graphicsEffect)
                widget->d_func()->dirty += widget->d_func()->effectiveRectFor(r);
            else
#endif
                widget->d_func()->dirty += r;
        }
    } else {
        addDirtyWidget(widget, r);
    }
    // ---------------------------------------------------------------------------
    if (updateTime == UpdateNow)
        sendUpdateRequest(tlw, updateTime);
}
template void QWidgetRepaintManager::markDirty<QRect>(const QRect &, QWidget *, UpdateTime, BufferState);
template void QWidgetRepaintManager::markDirty<QRegion>(const QRegion &, QWidget *, UpdateTime, BufferState);
void QWidgetRepaintManager::addDirtyWidget(QWidget *widget, const QRegion &rgn)
{
    if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
        QWidgetPrivate *widgetPrivate = widget->d_func();
#if QT_CONFIG(graphicseffect)
        if (widgetPrivate->graphicsEffect)
            widgetPrivate->dirty = widgetPrivate->effectiveRectFor(rgn.boundingRect());
        else
#endif // QT_CONFIG(graphicseffect)
            widgetPrivate->dirty = rgn;
        dirtyWidgets.append(widget);
        widgetPrivate->inDirtyList = true;
    }
}

update()函数并不立即执行刷新。

2.void repaint();

void QWidget::repaint()
{
    repaint(rect());
}

void QWidget::repaint(const QRect &rect)
{
    Q_D(QWidget);
    d->repaint(rect);
}

void QWidgetPrivate::repaint(T r)
{
    Q_Q(QWidget);
    if (!q->isVisible() || !q->updatesEnabled() || r.isEmpty())
        return;
    QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
    if (tlwExtra && tlwExtra->backingStore)
        tlwExtra->repaintManager->markDirty(r, q, QWidgetRepaintManager::UpdateNow);
}

repaint()函数为立即刷新。

3.void scroll(int dx, int dy);

void QWidget::scroll(int dx, int dy)
{
    if ((!updatesEnabled() && children().size() == 0) || !isVisible())
        return;
    if (dx == 0 && dy == 0)
        return;
    Q_D(QWidget);
#if QT_CONFIG(graphicsview)
    if (QGraphicsProxyWidget *proxy = QWidgetPrivate::nearestGraphicsProxyWidget(this)) {
        // Graphics View maintains its own dirty region as a list of rects;
        // until we can connect item updates directly to the view, we must
        // separately add a translated dirty region.
        for (const QRect &rect : d->dirty)
            proxy->update(rect.translated(dx, dy));
        proxy->scroll(dx, dy, proxy->subWidgetRect(this));
        return;
    }
#endif
    d->setDirtyOpaqueRegion();
    d->scroll_sys(dx, dy);
}

void QWidgetPrivate::scroll_sys(int dx, int dy)
{
    Q_Q(QWidget);
    scrollChildren(dx, dy);
    scrollRect(q->rect(), dx, dy);
}

void QWidgetPrivate::scrollRect(const QRect &rect, int dx, int dy)
{
    Q_Q(QWidget);
    QWidget *tlw = q->window();
    QTLWExtra* x = tlw->d_func()->topData();
    QWidgetRepaintManager *repaintManager = x->repaintManager.get();
    if (!repaintManager)
        return;
    static const bool accelEnv = qEnvironmentVariableIntValue("QT_NO_FAST_SCROLL") == 0;
    const QRect clipR = clipRect();
    const QRect scrollRect = rect & clipR;
    const bool accelerateScroll = accelEnv && isOpaque && !q_func()->testAttribute(Qt::WA_WState_InPaintEvent);
    if (!accelerateScroll) {
        if (!overlappedRegion(scrollRect.translated(data.crect.topLeft()), true).isEmpty()) {
            QRegion region(scrollRect);
            subtractOpaqueSiblings(region);
            invalidateBackingStore(region);
        }else {
            invalidateBackingStore(scrollRect);
        }
    } else {
        const QPoint toplevelOffset = q->mapTo(tlw, QPoint());
        const QRect destRect = scrollRect.translated(dx, dy) & scrollRect;
        const QRect sourceRect = destRect.translated(-dx, -dy);
        const QRegion overlappedExpose = (overlappedRegion(scrollRect.translated(data.crect.topLeft())))
                .translated(-data.crect.topLeft()) & clipR;
        QRegion childExpose(scrollRect);
        const qreal factor = QHighDpiScaling::factor(q->windowHandle());
        if (overlappedExpose.isEmpty() || qFloor(factor) == factor) {
            const QList<QRect> rectsToScroll =
                    getSortedRectsToScroll(QRegion(sourceRect) - overlappedExpose, dx, dy);
            for (const QRect &r : rectsToScroll) {
                if (repaintManager->bltRect(r, dx, dy, q)) {
                    childExpose -= r.translated(dx, dy);
                }
            }
        }
        childExpose -= overlappedExpose;
        if (inDirtyList) {
            if (rect == q->rect()) {
                dirty.translate(dx, dy);
            } else {
                QRegion dirtyScrollRegion = dirty.intersected(scrollRect);
                if (!dirtyScrollRegion.isEmpty()) {
                    dirty -= dirtyScrollRegion;
                    dirtyScrollRegion.translate(dx, dy);
                    dirty += dirtyScrollRegion;
                }
            }
        }
        if (!q->updatesEnabled())
            return;
        if (!overlappedExpose.isEmpty())
            invalidateBackingStore(overlappedExpose);
        if (!childExpose.isEmpty()) {
            repaintManager->markDirty(childExpose, q);
            isScrolled = true;
        }
        // Instead of using native scroll-on-screen, we copy from
        // backingstore, giving only one screen update for each
        // scroll, and a solid appearance
        repaintManager->markNeedsFlush(q, destRect, toplevelOffset);
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据提供的引用内容,以下是在QWidget中使用VLC-Qt进行截图的步骤: 1. 首先,确保已经按照引用中提供的步骤正确编译和配置了VLC-Qt。 2. 在你的QWidget类中,添加VlcInstance和VlcMediaPlayer对象作为成员变量,并在构造函数中进行初始化。 ```cpp #include <VLCQtCore/Instance.h> #include <VLCQtCore/MediaPlayer.h> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget *parent = nullptr) : QWidget(parent) { // 初始化VLC实例 vlcInstance = new VlcInstance(VlcCommon::args(), this); // 创建VLC媒体播放器 vlcMediaPlayer = new VlcMediaPlayer(vlcInstance); } private: VlcInstance *vlcInstance; VlcMediaPlayer *vlcMediaPlayer; }; ``` 3. 在需要截图的地方,使用VlcMediaPlayer的grabVideo()函数进行截图,并将截图保存到文件。 ```cpp QString saveScreenshot(const QString &filePath) { // 使用VlcMediaPlayer的grabVideo()函数进行截图 QImage screenshot = vlcMediaPlayer->grabVideo(); // 将截图保存到文件 screenshot.save(filePath); return filePath; } ``` 注意:在调用grabVideo()函数之前,确保VLC媒体播放器已经加载了媒体文件并开始播放。 4. 调用saveScreenshot()函数进行截图,并指定保存截图的文件路径。 ```cpp QString screenshotFilePath = saveScreenshot("screenshot.png"); ``` 这将保存截图为名为"screenshot.png"的文件。 5. 最后,你可以在QWidget中显示截图,例如使用QLabel来显示截图。 ```cpp QLabel *screenshotLabel = new QLabel(this); screenshotLabel->setPixmap(QPixmap(screenshotFilePath)); screenshotLabel->show(); ``` 这将在QWidget中显示截图。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值