我想设置文本绘图对齐,使用一个点作为坐标,而不是矩形。
据我所知QPainter::drawText
只有在我将坐标作为矩形传递时才允许设置文本对齐。
如果我希望相对于某个点而不是矩形对齐文本,我该如何设置文本对齐方式?
答案 0 :(得分:26)
当您传递绘图文本的起点时,您实际上是在一个大的矩形上绘制文本,该矩形在给定点处具有左下角。所以你需要的是根据你的起点和选择的对齐方式提供一个合适的“无限”矩形:
// https://github.com/KubaO/stackoverflown/tree/master/questions/alignments-24831484
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
void drawText(QPainter & painter, qreal x, qreal y, Qt::Alignment flags,
const QString & text, QRectF * boundingRect = 0)
{
const qreal size = 32767.0;
QPointF corner(x, y - size);
if (flags & Qt::AlignHCenter) corner.rx() -= size/2.0;
else if (flags & Qt::AlignRight) corner.rx() -= size;
if (flags & Qt::AlignVCenter) corner.ry() += size/2.0;
else if (flags & Qt::AlignTop) corner.ry() += size;
else flags |= Qt::AlignBottom;
QRectF rect{corner.x(), corner.y(), size, size};
painter.drawText(rect, flags, text, boundingRect);
}
void drawText(QPainter & painter, const QPointF & point, Qt::Alignment flags,
const QString & text, QRectF * boundingRect = {})
{
drawText(painter, point.x(), point.y(), flags, text, boundingRect);
}
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QLabel label;
QPicture pic;
pic.setBoundingRect({-100, -100, 200, 200});
QPainter p(&pic);
const QPointF pt;
p.drawEllipse(pt, 3, 3);
p.setFont({"Helvetica", 40});
p.setPen({128, 0, 0, 128});
drawText(p, pt, Qt::AlignBottom, "_LB");
drawText(p, pt, Qt::AlignVCenter, "_LC");
drawText(p, pt, Qt::AlignTop, "_LT");
p.setPen({0, 128, 0, 128});
drawText(p, pt, Qt::AlignBottom | Qt::AlignHCenter, "MB");
drawText(p, pt, Qt::AlignVCenter | Qt::AlignHCenter, "MC");
drawText(p, pt, Qt::AlignTop | Qt::AlignHCenter, "MT");
p.setPen({0, 0, 128, 128});
drawText(p, pt, Qt::AlignBottom | Qt::AlignRight, "RB_");
drawText(p, pt, Qt::AlignVCenter | Qt::AlignRight, "RC_");
drawText(p, pt, Qt::AlignTop | Qt::AlignRight, "RT_");
p.end();
label.setPicture(pic);
label.show();
return a.exec();
}
答案 1 :(得分:0)
我修改了现有答案(https://stackoverflow.com/a/24831796),使其更具自文档性,并在未指定垂直对齐的情况下增加了对“基线对齐”的支持。
d
开头的变量是增量向量。其他变量是绝对坐标。(不幸的是,如果我尝试在项目符号列表之后添加代码,格式就会搞砸。)
#include <QtGui>
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
#include <QtWidgets>
#endif
class BaselinePainter: public QPainter {
private:
int calcDescent() const {
QFontMetrics fontMetrics(this->font());
return fontMetrics.descent();
}
int descent = calcDescent();
public:
using QPainter::QPainter;
void setFont(const QFont &f) {
QPainter::setFont(f);
this->descent = calcDescent();
}
void drawTextAligned(qreal x, qreal y, Qt::Alignment align,
const QString & text, QRectF * boundingRect = 0)
{
const qreal dDown = 32767.0;
const qreal dRight = dDown;
qreal left = x;
qreal top = y;
if (align & Qt::AlignHCenter) {
left -= dRight/2.0;
}
else if (align & Qt::AlignRight) {
left -= dRight;
}
if (align & Qt::AlignTop) {
// do nothing
}
else if (align & Qt::AlignVCenter) {
top -= dDown/2.0;
}
else if (align & Qt::AlignBottom) {
top -= dDown;
}
else {
// Emulate baseline alignment (AKA calling drawText() with a point).
// https://code.woboq.org/qt5/qtbase/src/gui/painting/qpainter.cpp.html
// Qt drawText(rect) has a simple "no-shaping" mode (undocumented Qt::TextBypassShaping, will be removed in Qt 6)
// and a complex "glyph-script-shaping" mode.
// My code will only be using drawText() for ASCII characters.
// Each codepath computes font descent differently.
// The simple mode probably constructs one QFontEngine per call, to compute descent.
// The complex mode does weird things.
int dDescentDown = this->descent;
align |= Qt::AlignBottom;
top -= dDown;
top += dDescentDown;
}
QRectF rect{left, top, dRight, dDown};
this->drawText(rect, align, text, boundingRect);
}
void drawTextAligned(const QPointF & point, Qt::Alignment align,
const QString & text, QRectF * boundingRect = {})
{
drawTextAligned(point.x(), point.y(), align, text, boundingRect);
}
};
int main(int argc, char *argv[])
{
QApplication a{argc, argv};
QLabel label;
QPicture pic;
pic.setBoundingRect({-100, -100, 200, 200});
BaselinePainter p(&pic);
const QPointF pt;
p.drawEllipse(pt, 3, 3);
p.setFont({"Helvetica", 16});
p.setPen({128, 0, 0, 128});
p.drawTextAligned(pt, Qt::AlignBottom, "aagg");
p.drawTextAligned(pt, 0, "ga");
p.drawTextAligned(pt, Qt::AlignVCenter, "·");
p.drawTextAligned(pt, Qt::AlignTop, "↑");
p.setPen({0, 128, 0, 128});
p.drawTextAligned(pt, Qt::AlignBottom | Qt::AlignHCenter, "aagg");
p.drawTextAligned(pt, Qt::AlignHCenter, "ga");
p.drawTextAligned(pt, Qt::AlignVCenter | Qt::AlignHCenter, "·");
p.drawTextAligned(pt, Qt::AlignTop | Qt::AlignHCenter, "↑");
p.setPen({0, 0, 128, 128});
p.drawTextAligned(pt, Qt::AlignBottom | Qt::AlignRight, "↘");
p.drawTextAligned(pt, Qt::AlignVCenter | Qt::AlignRight, "→");
p.drawTextAligned(pt, Qt::AlignTop | Qt::AlignRight, "↗");
p.end();
label.setPicture(pic);
label.show();
return a.exec();
}