1
因此,我有一個QComboBox。QComboBox消除選定項目上的文字
如果currentText()過長小部件,然後我要顯示一個省略號。
像這樣:
所以:
void MyComboBox::paintEvent(QPaintEvent *)
{
QStylePainter painter(this);
QStyleOptionComboBox opt;
initStyleOption(&opt);
painter.drawComplexControl(QStyle::CC_ComboBox, opt);
QRect rect = this->rect();
//this is not ideal
rect.setLeft(rect.left() + 7);
rect.setRight(rect.width() - 15);
//
QTextOption option;
option.setAlignment(Qt::AlignVCenter);
QFontMetrics fontMetric(painter.font());
const QString elidedText = QAbstractItemDelegate::elidedText(fontMetric, rect.width(), Qt::ElideRight, this->currentText());
painter.drawText(rect, elidedText, option);
}
這是工作flawlessy。 問題是評論之間的代碼,因爲我硬編碼從左邊界和右邊界的距離。這讓我感到害怕。
無代碼的結果是:
有誰知道要做到這一點,沒有硬編碼更一般的方法是什麼? 謝謝
謝謝你的建議..只要我有一段時間我會嘗試調查你的建議! –
它完美無瑕!謝謝! –