布局中的小部件始终处于不重叠的状态,所以我只是看不到textLabel
可能重叠valueLabel
。很有可能您的小部件不受布局管理,即使它们已添加到布局中。也许带标签的布局不是另一个布局的子元素,或者未在容器小部件上设置。
你没有告诉我们什么。一个独立的测试用例应该是很好的。
如果您希望标签通过用“...”结束文本而不是突然切断文本,可以使用以下简化版式。
// Usage:
/*
QApplication app;
app.setStyle(new ElidedStyle);
...
QWidget * w = new QLabel("Hello World!");
w->setProperty("elidedItemText", true);
*/
// Interface
class ElidedStyle : public QProxyStyle
{
public:
static QString elidedText(const QString & text, QPainter * painter, const QRect & rect);
virtual void drawItemText(
QPainter * painter, const QRect & rect, int flags, const QPalette & pal,
bool enabled, const QString & text, QPalette::ColorRole textRole = QPalette::NoRole) const Q_DECL_OVERRIDE;
};
// Implementation
QString ElidedStyle::elidedText(const QString & text, QPainter * painter, const QRect & rect)
{
QWidget * widget = dynamic_cast<QWidget*>(painter->device());
if (widget && widget->property("elidedItemText").toBool()) {
QFontMetrics fm(painter->font());
return fm.elidedText(text, Qt::ElideMiddle, rect.width());
}
return text;
}
void ElidedStyle::drawItemText(
QPainter * painter, const QRect & rect, int flags, const QPalette & pal,
bool enabled, const QString & text, QPalette::ColorRole textRole) const
{
QProxyStyle::drawItemText(painter, rect, flags, pal, enabled, elidedText(text, painter, rect), textRole);
}
我创建了网格/水平布局,并将“水平策略=忽略”添加到最左侧标签的属性。它看起来没有重叠,相反,一旦最左边的标签(textLabel)变得太长,它会隐藏在中间标签(valueLabel)下面。 – wolf9000