佈局中的小部件始終處於不重疊的狀態,所以我只是看不到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