16,239
社区成员
发帖
与我相关
我的任务
分享
static void adjustBubbleBlockMargin(const QAbstractTextDocumentLayout::PaintContext &context, QTextBlock &bl)
{
bool ble = bl.blockFormat().hasProperty(QTextFormat::BubbleFormat);
if(!ble)
return ;
QTextCharFormat tcf = bl.charFormat();
QFontMetrics cfm(tcf.font());
QString str = bl.text();
QRect crect = cfm.boundingRect(str);
int textw = crect.width();
int texth = crect.height();
QSizeF s = bl.document()->size();
QTextCursor tc(bl);
QTextBlockFormat bf(bl.blockFormat());
bool friendBle = bl.blockFormat().hasProperty(QTextFormat::FriendBubbleFormat);
if(friendBle)
{
bf.setLeftMargin(50);
bf.setAlignment(Qt::AlignLeft);
}
else
{
if(context.clip.width() > 1024)
{
bf.setLeftMargin(50);
bf.setAlignment(Qt::AlignLeft);
}
else
{
bf.setAlignment(Qt::AlignLeft);
if(textw > context.clip.width())
{
bf.setRightMargin(40);
}
else
{
bf.setRightMargin(40);
bf.setLeftMargin(context.clip.width() - textw - 57);
}
}
}
tc.setBlockFormat(bf);//这句话,如果执行的话,单核CPU会打满,目前还没有找到在哪里修改这个QTextBlockFormat才是合适的
}