gpt4 book ai didi

c++ - 根据比例计算 QGraphicsTextItem 字体大小

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:26 26 4
gpt4 key购买 nike

我有QGraphicsTextItem QGraphicsScene 上的对象.用户可以缩放 QGraphicsTextItem通过拖动角来移动对象。 (我正在使用自定义“转换编辑器”来执行此操作。)用户还可以更改 QGraphicsTextItem 的大小。通过从属性面板更改字体大小。我想做的是统一这些,以便当用户通过用鼠标拖动角来缩放对象时,它实际上在幕后计算“需要什么大小的字体才能使生成的对象适合目标大小并保持比例因子为 1.0?”

我现在正在做的是使用 QGraphicsItem::mouseMoveEvent 让对象正常缩放然后触发 FinalizeMapScale QGraphicsItem::mouseReleaseEvent 中的方法一旦鼠标秤完成。然后,此方法应将字体更改为适当的大小并将比例设置回 1.0。

我有一个似乎有效的解决方案,但我并不热衷于此。我对 Qt 和 C++ 都比较陌生,因此不胜感激任何评论或更正。

  • 有没有更好的方法来设计这一切?
  • 是否已经有 Qt 方法可以做到这一点?
  • 我的方法是否正确,但有一些 Qt 或 C++ 错误?

请在下面对我的回答发表评论,提交您自己喜欢的解决方案。谢谢!

[编辑] 根据评论中的要求,这里是缩放代码的基础知识。实际上,我们对此采取了不同的方向,因此不再使用这段代码(以及下面的代码)。此代码在 mouseMoveEvent 中方法,之前在 mousePressEvent 中将“scaling_”标志设置为 true如果在右下角的“热点”中单击了鼠标。请注意,此代码位于装饰器 QGraphicsItem 中,该装饰器包含指向其正在缩放的​​目标的指针。这种抽象对于我们的项目来说是必要的,但对于大多数用途来说可能有点矫枉过正。

void TransformDecorator::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
...
if (scaling_) {
QGraphicsItem *target_item = target_->AsQGraphicsItem();
target_item->setTransformOriginPoint(0.0, 0.0);
QPointF origin_scene = mapToScene(target_item->transformOriginPoint());
QPointF scale_position_scene = mapToScene(event->pos());
qreal unscaled_width = target_item->boundingRect().width();
qreal scale_x = (scale_position_scene.x() - origin_scene.x()) / unscaled_width;
if (scale_x * unscaled_width < kMinimumSize) {
scale_x = kMinimumSize / unscaled_width;
}
target_item->setScale(scale_x);
} else {
QGraphicsObject::mouseMoveEvent(event);
}
}

最佳答案

请不要对 loop-with-exit 结构大打出手。我们对此很满意。

void MapTextElement::FinalizeMapScale() {

// scene_document_width is the width of the text document as it appears in
// the scene after scaling. After we are finished with this method, we want
// the document to be as close as possible to this width with a scale of 1.0.
qreal scene_document_width = document()->size().width() * scale();

QString text = toPlainText();

// Once the difference between scene_document_width and the calculated width
// is below this value, we accept the new font size.
const qreal acceptable_delta = 1.0;

// If the difference between scene_document_width and the calculated width is
// more than this value, we guess at the new font size by calculating a new
// scale factor. Once it is beneath this value, we creep up (or down) by tiny
// increments. Without this, we would sometimes incur long "back and forth"
// loops when using the scale factor.
const qreal creep_delta = 8.0;
const qreal creep_increment = 0.1;

QScopedPointer<QTextDocument> test_document(document()->clone());
QFont new_font = this->font();
qreal delta = 0.0;

// To prevent infinite loops, we store the font size values that we try.
// Because of the unpredictable (at least to me) relationship between font
// point size and rendering size, this was the only way I could get it to
// work reliably.
QList<qreal> attempted_font_sizes;

while (true) {

test_document->setDefaultFont(new_font);
delta = scene_document_width - test_document->size().width();

if (std::abs(delta) <= acceptable_delta ||
attempted_font_sizes.contains(new_font.pointSizeF())) {
break;
}

attempted_font_sizes.append(new_font.pointSizeF());

qreal new_font_size = 0.0;
if (std::abs(delta) <= creep_delta) {
new_font_size = delta > 0.0 ? new_font.pointSizeF() + creep_increment
: new_font.pointSizeF() - creep_increment;
} else {
new_font_size = new_font.pointSizeF()
* scene_document_width
/ test_document->size().width();
}
new_font.setPointSizeF(new_font_size);
}

this->setFont(new_font);
this->setScale(1.0);
}

关于c++ - 根据比例计算 QGraphicsTextItem 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3302086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com