// 计算文本的大致尺寸 QFontMetrics fm(textEditor->font()); QRect textRect = fm.boundingRect(textItem->toPlainText()); // 设置编辑框大小,增加一些边距 const int margin = 10; textEditor->setGeometry( center.x() - textRect.width()/2 - margin, center.y() - textRect.height()/2 - margin, textRect.width() + margin*2, textRect.height() + margin*2 );