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;
}
}