QT源码拾贝0-5(qimage和qpainter)

简介: 这篇文章介绍了在Qt源码中qimage和qpainter的使用,包括线程池的使用、智能指针的存储、std::exchange函数的应用、获取类对象的方法以及QChar字节操作。

0 qt源码查看方法

使用vscode工具,加载qt源码路径,比如:C:\Qt\6.5.0\Src

再安装一个C/C++插件:

按住ctrl+鼠标,可以方便查看引用函数的定义。

返回上次光标位置,使用快捷键Alt和方向左/右键

跳过单个变量,使用快捷键ctrl加方向左/右键

1. qimage.cpp中线程池使用方法


/*!
    \since 5.14

    Applies the color transformation \a transform to all pixels in the image.
*/
void QImage::applyColorTransform(const QColorTransform &transform)
{
   
    if (transform.isIdentity())
        return;
    detach();
    if (!d)
        return;
    if (pixelFormat().colorModel() == QPixelFormat::Indexed) {
   
        for (int i = 0; i < d->colortable.size(); ++i)
            d->colortable[i] = transform.map(d->colortable[i]);
        return;
    }
    QImage::Format oldFormat = format();
    if (qt_fpColorPrecision(oldFormat)) {
   
        if (oldFormat != QImage::Format_RGBX32FPx4 && oldFormat != QImage::Format_RGBA32FPx4
                && oldFormat != QImage::Format_RGBA32FPx4_Premultiplied)
            convertTo(QImage::Format_RGBA32FPx4);
    } else if (depth() > 32) {
   
        if (oldFormat != QImage::Format_RGBX64 && oldFormat != QImage::Format_RGBA64
                && oldFormat != QImage::Format_RGBA64_Premultiplied)
            convertTo(QImage::Format_RGBA64);
    } else if (oldFormat != QImage::Format_ARGB32 && oldFormat != QImage::Format_RGB32
                && oldFormat != QImage::Format_ARGB32_Premultiplied) {
   
        if (hasAlphaChannel())
            convertTo(QImage::Format_ARGB32);
        else
            convertTo(QImage::Format_RGB32);
    }

    QColorTransformPrivate::TransformFlags flags = QColorTransformPrivate::Unpremultiplied;
    switch (format()) {
   
    case Format_ARGB32_Premultiplied:
    case Format_RGBA64_Premultiplied:
    case Format_RGBA32FPx4_Premultiplied:
        flags = QColorTransformPrivate::Premultiplied;
        break;
    case Format_RGB32:
    case Format_RGBX64:
    case Format_RGBX32FPx4:
        flags = QColorTransformPrivate::InputOpaque;
        break;
    case Format_ARGB32:
    case Format_RGBA64:
    case Format_RGBA32FPx4:
        break;
    default:
        Q_UNREACHABLE();
    }

    std::function<void(int,int)> transformSegment;

    if (qt_fpColorPrecision(format())) {
   
        transformSegment = [&](int yStart, int yEnd) {
   
            for (int y = yStart; y < yEnd; ++y) {
   
                QRgbaFloat32 *scanline = reinterpret_cast<QRgbaFloat32 *>(d->data + y * d->bytes_per_line);
                QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
            }
        };
    } else  if (depth() > 32) {
   
        transformSegment = [&](int yStart, int yEnd) {
   
            for (int y = yStart; y < yEnd; ++y) {
   
                QRgba64 *scanline = reinterpret_cast<QRgba64 *>(d->data + y * d->bytes_per_line);
                QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
            }
        };
    } else {
   
        transformSegment = [&](int yStart, int yEnd) {
   
            for (int y = yStart; y < yEnd; ++y) {
   
                QRgb *scanline = reinterpret_cast<QRgb *>(d->data + y * d->bytes_per_line);
                QColorTransformPrivate::get(transform)->apply(scanline, scanline, width(), flags);
            }
        };
    }

#if QT_CONFIG(thread) && !defined(Q_OS_WASM)
    int segments = (qsizetype(width()) * height()) >> 16;
    segments = std::min(segments, height());
    QThreadPool *threadPool = QThreadPool::globalInstance();
    if (segments > 1 && threadPool && !threadPool->contains(QThread::currentThread())) {
   
        QSemaphore semaphore;
        int y = 0;
        for (int i = 0; i < segments; ++i) {
   
            int yn = (height() - y) / (segments - i);
            threadPool->start([&, y, yn]() {
   
                transformSegment(y, y + yn);
                semaphore.release(1);
            });
            y += yn;
        }
        semaphore.acquire(segments);
    } else
#endif
        transformSegment(0, height());

    if (oldFormat != format())
        *this = std::move(*this).convertToFormat(oldFormat);
}

2. qpainter_p.h中SmallStack模板元结构体存放智能指针


    std::unique_ptr<QPainterState> state;
    template <typename T, std::size_t N = 8>
    struct SmallStack : std::stack<T, QVarLengthArray<T, N>> {
   
        void clear() {
    this->c.clear(); }
    };
    SmallStack<std::unique_ptr<QPainterState>> savedStates;

    mutable std::unique_ptr<QPainterDummyState> dummyState;

3. qpainter.cpp的保存函数,状态对象赋值使用std::exchange函数

void QPainter::save()
{
   
#ifdef QT_DEBUG_DRAW
    if (qt_show_painter_debug_output)
        printf("QPainter::save()\n");
#endif
    Q_D(QPainter);
    if (!d->engine) {
   
        qWarning("QPainter::save: Painter not active");
        return;
    }

    std::unique_ptr<QPainterState> prev;
    if (d->extended) {
   
        // separate the creation of a new state from the update of d->state, since some
        // engines access d->state directly (not via createState()'s argument)
        std::unique_ptr<QPainterState> next(d->extended->createState(d->state.get()));
        prev = std::exchange(d->state, std::move(next));
        d->extended->setState(d->state.get());
    } else {
   
        d->updateState(d->state);
        prev = std::exchange(d->state, std::make_unique<QPainterState>(d->state.get()));
        d->engine->state = d->state.get();
    }
    d->savedStates.push(std::move(prev));
}

4. qpainter.cpp中获得类对象的方法


QBrush QPaintEngineState::brush() const
{
   
    return static_cast<const QPainterState *>(this)->brush;
}

5. qpainter.cpp中QChar字节操作,使用u在字节前面

int old_offset = offset;
    for (; offset < text.size(); offset++) {
   
        QChar chr = text.at(offset);
        if (chr == u'\r' || (singleline && chr == u'\n')) {
   
            text[offset] = u' ';
        } else if (chr == u'\n') {
   
            text[offset] = QChar::LineSeparator;
        } else if (chr == u'&') {
   
            ++maxUnderlines;
        } else if (chr == u'\t') {
   
            if (!expandtabs) {
   
                text[offset] = u' ';
            } else if (!tabarraylen && !tabstops) {
   
                tabstops = qRound(fm.horizontalAdvance(u'x')*8);
            }
        } else if (chr == u'\x9c') {
   
            // string with multiple length variants
            hasMoreLengthVariants = true;
            break;
        }
    }

    QList<QTextLayout::FormatRange> underlineFormats;
    int length = offset - old_offset;
    if ((hidemnmemonic || showmnemonic) && maxUnderlines > 0) {
   
        QChar *cout = text.data() + old_offset;
        QChar *cout0 = cout;
        QChar *cin = cout;
        int l = length;
        while (l) {
   
            if (*cin == u'&') {
   
                ++cin;
                --length;
                --l;
                if (!l)
                    break;
                if (*cin != u'&' && !hidemnmemonic && !(tf & Qt::TextDontPrint)) {
   
                    QTextLayout::FormatRange range;
                    range.start = cout - cout0;
                    range.length = 1;
                    range.format.setFontUnderline(true);
                    underlineFormats.append(range);
                }
#ifdef Q_OS_MAC
            } else if (hidemnmemonic && *cin == u'(' && l >= 4 &&
                       cin[1] == u'&' && cin[2] != u'&' &&
                       cin[3] == u')') {
   
                int n = 0;
                while ((cout - n) > cout0 && (cout - n - 1)->isSpace())
                    ++n;
                cout -= n;
                cin += 4;
                length -= n + 4;
                l -= 4;
                continue;
#endif //Q_OS_MAC
            }
            *cout = *cin;
            ++cout;
            ++cin;
            --l;
        }
    }
相关文章
|
11天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
8天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2522 18
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
8天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1525 15
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
|
4天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
10天前
|
编解码 JSON 自然语言处理
通义千问重磅开源Qwen2.5,性能超越Llama
击败Meta,阿里Qwen2.5再登全球开源大模型王座
590 14
|
1月前
|
运维 Cloud Native Devops
一线实战:运维人少,我们从 0 到 1 实践 DevOps 和云原生
上海经证科技有限公司为有效推进软件项目管理和开发工作,选择了阿里云云效作为 DevOps 解决方案。通过云效,实现了从 0 开始,到现在近百个微服务、数百条流水线与应用交付的全面覆盖,有效支撑了敏捷开发流程。
19283 30
|
10天前
|
人工智能 自动驾驶 机器人
吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
过去22个月,AI发展速度超过任何历史时期,但我们依然还处于AGI变革的早期。生成式AI最大的想象力,绝不是在手机屏幕上做一两个新的超级app,而是接管数字世界,改变物理世界。
491 49
吴泳铭:AI最大的想象力不在手机屏幕,而是改变物理世界
|
1月前
|
人工智能 自然语言处理 搜索推荐
阿里云Elasticsearch AI搜索实践
本文介绍了阿里云 Elasticsearch 在AI 搜索方面的技术实践与探索。
18842 20
|
1月前
|
Rust Apache 对象存储
Apache Paimon V0.9最新进展
Apache Paimon V0.9 版本即将发布,此版本带来了多项新特性并解决了关键挑战。Paimon自2022年从Flink社区诞生以来迅速成长,已成为Apache顶级项目,并广泛应用于阿里集团内外的多家企业。
17530 13
Apache Paimon V0.9最新进展
|
2天前
|
云安全 存储 运维
叮咚!您有一份六大必做安全操作清单,请查收
云安全态势管理(CSPM)开启免费试用
367 4
叮咚!您有一份六大必做安全操作清单,请查收