QComboBoxです。QComboBox選択した商品にテキストが表示されません
currentText()は、ウィジェットの長すぎる場合、私は省略記号を表示したいです。このよう
:だから
:
これはflawlessyに取り組んでいる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);
}
。 問題は、左右の境界からの距離をハードコーディングしているため、コメントの間のコードです。それは私をうんざらさせる。そのコードなし
結果は次のとおりです。
誰もがハードコーディングせずに、これを行うには、より一般的な方法を知っていますか? ありがとうございました
提案していただきありがとうございます。しばらくするとすぐに、私があなたの提案したものを調査しようとします! –
それは完璧に動作します!ありがとうございました! –