- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要使用 创建文本项文本内容的精确宽度和高度 .
文字的高度是最重要的要求。
文本的位置应该相对于文本本身。
我还必须能够将它放置在 Canvas 上的确切位置。
假设一个(可打印的) Canvas (在更大的 QGraphicsScene
上),比如 5 英寸宽和 1 英寸高,我的文本应该能够从上到下从左到右拉伸(stretch) - 并被放置在 Canvas 上,而不是部分分开.
我正在分类 QGraphicsTextItem
对于我的项目类型。我正在调整它的大小,使用 QTransform()
, 到所需尺寸 - 以英寸或毫米或像素 (72*in) 为单位。
同时设置 document()
边距为 0,内部的任何内容(如 QTextBlockFormat
边距)也为 0。
我已经实现了 setItemSize(QSizeF sz)
(以像素为单位的 sz),调整 QGraphicsTextItem
的大小按要求。
使用项目边界矩形初始化 sz。
假设没有换行,单行文本(一旦这个问题得到解决,多行可以单独解决)。
将项目添加到 Canvas 时,我仍然看到顶部和底部边距 - 这取决于字体选择。
我在项目周围画了一个矩形来查看它。
顶部/底部距离取决于字体选择。
我尝试使用字体度量来确定这些距离(在 paint()
中,我一直在画线以尝试确定文本适合的位置和矩形)。
我很高兴至少能够确定正确的大小以用于大写,没有重音或特殊字符字体(这将是一个开始,尽管我自然需要能够使用任何字符)。
但至少有一些方法可以确定大小和位置 (相对于项目的(0,0))即使在最简单的情况下也是如此.....
字体规范tightBoundingRect()
似乎是最准确的尺寸,但似乎不可能确定它的位置,以便我可以以某种方式正确创建我的项目,并可能正确调整它们的大小/移动它们以适合 Canvas 。
以下是我努力确定相对于项目的 (0,0) 的文本的至少确切大小和位置的一些示例(假设一旦我这样做,我就能够将该信息暴露给外部或包括转变在调整大小的项目变换中)。
请注意,字体度量所宣传的文本大小并不总是覆盖文本,并且对于不同的字体,我无法将紧密的边界矩形(洋红色)定位在文本本身周围。 (我做了多次猜测,下面的代码只是一个 - 这些行试图显示不同的字体度量大小)。
以上是继承QGraphicsTextItem
的文本项的绘制功能的实验。 :
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
// draw text
QGraphicsTextItem::paint(painter, option, widget);
QPen p;
p.setWidthF(0);
QFontMetricsF fm(this->font());
qreal ascent = fm.ascent(),
descent = fm.descent(),
hheight = fm.height();
QRectF r = QGraphicsTextItem::boundingRect();
QRectF rFont= fm.tightBoundingRect(toPlainText());
qreal xmax = r.right();
painter->save();
painter->setBrush(Qt::NoBrush);
// where is "ascent + descent"
p.setColor(Qt::green);
painter->setPen(p);
painter->drawLine(QPointF(2, ascent), QPointF(2, ascent + descent));
painter->drawLine(QPointF(2, ascent + descent), QPointF(xmax/2, ascent + descent));
// where is "height"
p.setColor(Qt::red);
painter->setPen(p);
painter->drawLine(QPointF(xmax/2, 0), QPointF(xmax/2, hheight));
painter->drawLine(QPointF(xmax/2, ascent + descent), QPointF(xmax, ascent + descent));
// where is "ascent"
p.setColor(Qt::yellow);
painter->setPen(p);
painter->drawLine(QPointF(6, 0), QPointF(6, ascent));
painter->drawLine(QPointF(6, ascent), QPointF(xmax, ascent));
// something that may look like top of the text
p.setColor(Qt::blue);
painter->setPen(p);
qreal yyy = ascent + rFont.y() + 1;
painter->drawLine(QPointF(5, yyy), QPointF(xmax, yyy));
// this should be useful... should be the natural offset
qreal yoffset = (r.height() - rFont.height()) / 2;
// qDebug() << yoffset << r << rFont;
//qreal y0 = (r.height() - fm.height())/2;
p.setColor(Qt::darkGreen);
painter->drawEllipse(10, yoffset, 1, 1);
// where is the font rect
p.setColor(Qt::magenta);
painter->setPen(p);
yoffset = (r.height() + rFont.height()) / 2;
painter->translate(0, yoffset);
painter->drawRect(rFont);
painter->restore();
}
QGraphicsTextItem
,但在矩形内绘制文本。同样的事情也会发生。
最佳答案
这不是一个好的解决方案。 这是尝试解决我自己的问题 - 在第一次迭代中 - 使用字体度量设置具有给定宽度和高度的文本。
不好的原因——
myText->setItemFontSize(12); // If I use font metrics I need to reset text size on every change, because resizing loses font info
QFontMetricsF fm(myText->font());
QRectF fmRect = fm.tightBoundingRect(myText.toPlainText().toUpper());
// without toUpper() the size is too small - even so it is a bit small
// I read tightBoundingRect is slow - but boundingRect and height and ascent all give values that result in even smaller size
//qreal absH = fm.ascent();
qreal absH = fmRect.height();
qreal absW = fmRect.width();
qreal absHeightRatio = myText->getItemSize().height() / absH;
qreal absWidthRatio = myText->getItemSize().width() / absW;
myText->setItemSize(QSizeF(absWidthRatio * textLength, absHeightRatio * fontHeight));
// This function scales the `QTransform` on item
// but since I request a final position dependent on item size
// (including blank space around it) - it has no chance of being accurate.....
// This is where my next effort will go, figuring out how to get rid of the fluff
// around the item inside the scaling
// leftShift = 10, rightShift = 10 in example
myText->setPos(0,0);
QRectF r = myText->mapToScene(myText->boundingRect()).boundingRect();
QSizeF sz = r.size();
qreal w = sz.width();
qreal h = sz.height();
qreal cx = (m_docLength - w + leftShift - rightShift)/2 - r.left();
qreal cy = (m_docHeight - h)/2 - r.top();
myText->setPos(cx, cy);
QGraphicsTextItem
的方式,这似乎更容易似乎定位它
关于qt - 设置精确高度和宽度的 QGraphicsTextItem 文本内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37736412/
基本上,我想知道为什么文本对齐标志和 setPageSize 的组合不会以文本居中显示结束。 下面的程序几乎完全符合我的要求,只是文本最终仅水平居中。 #include #include #inc
我想使 QGraphicsTextItem 在双击时可编辑,并在我单击时使其可移动。 #include #include #include #include class TextItem: p
我必须将一些波斯语文本写入 QGraphicsTextItem,但我找不到如何让 QGraphicsTextItem 写入 RightToLeft! 任何人都可以帮助我吗? 我试过这种方式,但它对我不
我想添加一个 QGraphicsTextItem 并且我想更改背景的颜色。我的意思是我希望包含文本的 boundingRect 具有特定颜色。一种方法是创建一个 QGraphicsRectItem 并
我真的被一个与 Qt GraphicsView 相关的任务困住了。任何帮助或建议将不胜感激。 在我的 QGraphicsView 应用程序中,我添加了一些可编辑的 QGraphicsTextItems
我需要使用 创建文本项文本内容的精确宽度和高度 . 文字的高度是最重要的要求。 文本的位置应该相对于文本本身。 我还必须能够将它放置在 Canvas 上的确切位置。 假设一个(可打印的) Canvas
在我软件的“关于”框中,我使用了 QGraphicsTextItem显示关于文本。 此文本包含超文本链接(形式为:link)。 该项目正确显示(超文本链接为蓝色并带有下划线)。但是,当我点击它们时,没
如何将 QGraphicsTextItem 转换/绘制成 QPixmap? 最佳答案 您可以将它添加到 QGraphicsScene(如果它不在其中),然后使用 render() 将场景添加到 QPi
我对 C++ 有点陌生,但在确定为什么我在以下代码中遇到段错误时遇到了一些麻烦; 游戏场景.h #ifndef GAMESCENE_H #define GAMESCENE_H #include cl
我和我的一个 friend 目前正在尝试使用 Qt 在 C++ 中制作游戏。我们当前的问题是,我们需要从按钮 mousePressEvent 上的 QGraphicsTextItem 获取文本。在游戏
我需要一个对象,它结合了 QGraphicsPixmapItem 和 QGraphicsTextItem(图像 + 文本)的特性。 多重继承是最好的方法还是有任何禁忌症?如果有,您有什么建议? 最佳答
我有QGraphicsTextItem QGraphicsScene 上的对象.用户可以缩放 QGraphicsTextItem通过拖动角来移动对象。 (我正在使用自定义“转换编辑器”来执行此操作。)
是否可以阻止右键单击打开 QGraphicsTextItem 上的默认上下文菜单?带有“撤消、重做、剪切、复制、粘贴...”的菜单。在 Ubuntu 18.04 上,就是这样。我不知道这在 Windo
我有一个 QGraphicsTextItem 作为一个 lineedit,使用 setTextInteractionFlags(Qt::TextEditorInteraction); 但是,如果用户按
让这个小片段: import sys from PyQt5 import QtWidgets from PyQt5 import QtCore from PyQt5 import QtGui from
我有一个包含多个 (QGraphicsTextItem) 的场景,我需要控制它们的颜色,那么如何更改 QGraphicsTextItem 的颜色?有可能吗?到目前为止,我已经尝试了 3 天。请帮忙 提
我想要一个 python 应用程序,它显示一堆小肖像和下面的名字。像那样: 它们应该是可移动和可编辑的(通过双击文本)。 我正在使用 PyQt4,所以我发现,为 Canvas 使用 QGraphics
我正在 Windows XP 上使用 QGraphicsScene 开发 Qt 4.8 应用程序。当用户双击 QGraphicsTextItem 时,我调用 textItem->setTextInte
我正在为一个添加了框架的 QGraphicsTextItem 制作动画。在动画期间,文本似乎在框架内轻微晃动,这非常烦人。 示例代码: class MovingFrameText : public Q
如果我想在 QGraphicsView 中放置一个字母(QGraphicsTextItem),有什么方法可以将文本设置为显示在我指定的坐标中间?如果我使用 QGraphicsTextItem.setP
我是一名优秀的程序员,十分优秀!