1 year ago

#382825

test-img

tushar

How to detect / avoid overlapping of text in Qt?

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 ?

c++

qt

qt5

qt4

qgraphicsview

0 Answers

Your Answer

Accepted video resources