如何在qt中使用paint()中的qpainter :: setTransform?

发布于 2025-01-20 16:30:00 字数 1888 浏览 0 评论 0原文

我有一个简单的 QGraphicsView,上面写有文本。该文本被包裹在一个边界矩形下。我已使用 QFontMetrics::elidedText 截断了文本。
当最初加载视图时,它会正确显示所有截断的文本。但是当我放大时,只有少数被截断的文本可见。如果我再放大几次,那么有时它会显示所有被截断的文本,有时它永远不会显示任何被截断的文本。
在添加截断的文本代码之前,zoom-in 工作正常。

widget.cpp

  void widget::ZoomIn()
  {
      double scaleFactor = 1.1;
      view->scale(scaleFactor,scaleFactor);
  }
void Widget::on_textButton_clicked()
{
    //logic to get string s and its co-ordinates firstPos and secondPos
    Text* t = new Text() ;
    QGraphicsTextItem* text = t->drawText(s,firstPos,secondPos,50,40);
    scene->addItem(text);
}       
  

Text.cpp

Text::Text(const QString &text,int left,int top,int width,int height)
    : QGraphicsTextItem (text),currentString(text),
    Left(left),Top(top),Width(width),Height(height)
{}     

Text::Text(){}

QGraphicsTextItem  *Text::drawText(QString s, int left,int top,int width,int height)
{
    QGraphicsTextItem  *text = new Text(s,left,top,width,height);
    return text;
}     

void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    QFont defaultFont = QApplication::font();
    QFontMetrics fm = QFontMetrics(defaultFont);
    QRect rect = QRect(Left,Top,Width,Height);
    QString elidedText = fm.elidedText(currentString, Qt::ElideLeft,rect.width());
    
    QTransform trans = view->transform();
    QTransform prevTrans;
    painter->setTransform(trans);
    painter->drawText(rect,Qt::AlignRight,elidedText);
    painter->setTransform(prevTrans);
    
}     
  

我明白,在 zoom-in 中,我正在更改转换,而画家正在旧的转换上进行绘制。所以我必须从 view 进行转换并使用 painter 设置它,然后使用 drawText() 然后设置 painter 转换与之前的转换一样。
我做了,但问题仍然存在。

I have a simple QGraphicsView with text written over it.That text is wrapped under a bounding rect. I have truncated a text using QFontMetrics::elidedText.
When initially view gets loaded, it shows all truncated text properly. But when I zoom-in only few truncated text gets visible. If for few more times I zoomed-in, then some times it shows all the truncated text and some times , it never shows any truncated text.
Before truncated text code gets added, zoom-in was working properly.

widget.cpp

  void widget::ZoomIn()
  {
      double scaleFactor = 1.1;
      view->scale(scaleFactor,scaleFactor);
  }
void Widget::on_textButton_clicked()
{
    //logic to get string s and its co-ordinates firstPos and secondPos
    Text* t = new Text() ;
    QGraphicsTextItem* text = t->drawText(s,firstPos,secondPos,50,40);
    scene->addItem(text);
}       
  

Text.cpp

Text::Text(const QString &text,int left,int top,int width,int height)
    : QGraphicsTextItem (text),currentString(text),
    Left(left),Top(top),Width(width),Height(height)
{}     

Text::Text(){}

QGraphicsTextItem  *Text::drawText(QString s, int left,int top,int width,int height)
{
    QGraphicsTextItem  *text = new Text(s,left,top,width,height);
    return text;
}     

void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    QFont defaultFont = QApplication::font();
    QFontMetrics fm = QFontMetrics(defaultFont);
    QRect rect = QRect(Left,Top,Width,Height);
    QString elidedText = fm.elidedText(currentString, Qt::ElideLeft,rect.width());
    
    QTransform trans = view->transform();
    QTransform prevTrans;
    painter->setTransform(trans);
    painter->drawText(rect,Qt::AlignRight,elidedText);
    painter->setTransform(prevTrans);
    
}     
  

I understood that, in zoom-in I am changing transformation and painter is drawing on old transformation. So I have to take transformation from view and set it with painter then drawText() and then set painter transformation as previous transformation.
I did , but still problem persist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文