如何检测/避免 Qt 中的文本重叠?
我有 QGraphicsView
,它有多个 QGraphicsItem
。在 QGraphicsView
上,我正在执行多种转换,例如 zoom-in
、zoom-out
、Fit-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 becomenon 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论