如何检测/避免 Qt 中的文本重叠?

发布于 2025-01-19 09:54:34 字数 1925 浏览 4 评论 0原文

我有 QGraphicsView,它有多个 QGraphicsItem。在 QGraphicsView 上,我正在执行多种转换,例如 zoom-inzoom-outFit-in 等。 br> 在 QGraphicsItem 中,我有几个矩形和一些折线。多段线连接到矩形。折线的位置彼此非常接近。每条折线上方都写有自己的名称。因此,当设计加载时,多段线的名称会相互重叠。如果我放大到特定级别,折线之间的距离会使它们的名称不重叠

所以我想检测文本的重叠。如果文本重叠,我不应该 在折线上显示它们。一旦我放大,当文本变得不重叠时,折线的名称应该是可见的。

我是这样想的。
每次放大缩小之后,我都会计算交集。

void myClass :: addItems() 
 {
       QGraphicsTextItem *text1 = new QGraphicsTextItem("line1");
       text1->setPos(20,40);                 
       text1->setDefaultTextColor(Qt::black);
       scene->addItem(text1);
 }
    
void myClass::ZoomIn()
 {
      double scaleFactor = 1.1;
      view->scale(scaleFactor,scaleFactor);
      Intersection();
 }

 void myClass:: Intersection()
 {
      QList<QGraphicsTextItem*> textList;
      _isIntersect = false;
      foreach(QGraphicsItem* currentItem, _scene->items())
      {
          QGraphicsTextItem* tItem = qgraphicsitem_cast<QGraphicsTextItem*>(currentItem);
          if(tItem)
          {
              textList.push_back(tItem);
          }
      }
      for(int i = 0 ; i < textList.size(); i++)
      {
           QGraphicsTextItem* iItem = textList[i];
           for( int j = i+1; j < textList.size(); j++)
           {
              QGraphicsTextItem* jItem = textList[j];
              if(iItem->boundingRect().intersects(jItem->boundingRect()))
              {
                  _isIntersect = true;
                  break;
              }
            }
            if(_isIntersect)
               break;
        }
    
        if(!_isIntersect)
            qDebug()<<"Not intersected ";
        else
            qDebug()<<"Intersected ";
}    

但对于每个视图,上述逻辑都显示“相交”。我哪里错了?

I am having QGraphicsView, which has multiple QGraphicsItem's. On QGraphicsView I am performing multiple transformation like zoom-in, zoom-out, Fit-in etc.
In QGraphicsItem, I am having few rectangles and some polylines. Polylines are attached to rectangles. The positions of polylines are very near to each other. Every polyline has its own name written above it. So when the design loads, names of the polylines overlap with each other. If I zoomed-in till particular level the distance between polylines makes their names non overlapping.

So I want to detect overlapping of text. If text overlap, I should not
show them on polyline. Once I zoomed in, and when text become non overlapping, names of polylines should be visible.

I thought like this.
After every zoom-in and zoom-out I am calculating for intersection.

void myClass :: addItems() 
 {
       QGraphicsTextItem *text1 = new QGraphicsTextItem("line1");
       text1->setPos(20,40);                 
       text1->setDefaultTextColor(Qt::black);
       scene->addItem(text1);
 }
    
void myClass::ZoomIn()
 {
      double scaleFactor = 1.1;
      view->scale(scaleFactor,scaleFactor);
      Intersection();
 }

 void myClass:: Intersection()
 {
      QList<QGraphicsTextItem*> textList;
      _isIntersect = false;
      foreach(QGraphicsItem* currentItem, _scene->items())
      {
          QGraphicsTextItem* tItem = qgraphicsitem_cast<QGraphicsTextItem*>(currentItem);
          if(tItem)
          {
              textList.push_back(tItem);
          }
      }
      for(int i = 0 ; i < textList.size(); i++)
      {
           QGraphicsTextItem* iItem = textList[i];
           for( int j = i+1; j < textList.size(); j++)
           {
              QGraphicsTextItem* jItem = textList[j];
              if(iItem->boundingRect().intersects(jItem->boundingRect()))
              {
                  _isIntersect = true;
                  break;
              }
            }
            if(_isIntersect)
               break;
        }
    
        if(!_isIntersect)
            qDebug()<<"Not intersected ";
        else
            qDebug()<<"Intersected ";
}    

But for every view, above logic is showing "Intersected". Where am I wrong ?

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

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

发布评论

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