Set QComboBox popup's size
-
Hello,
I'm trying to make an comboBox, based on QComboBox.
I've overloaded paintEvent and it paints well.Now, I'm trying to elide text inside the popup.
I've overloaded a delegate and it works well, looks elided and shofter.
However, I can't set the popup's width and it's not resized, it's ugly.
What can I do?
-
[Continuation of description]
This is my Combo class
void ComboWithElision::paintEvent(QPaintEvent *) { QStyleOptionComboBox option; initStyleOption(&option); QStylePainter painter(this); painter.drawComplexControl(QStyle::CC_ComboBox, option); auto comboRect = style()->subControlRect(QStyle::CC_ComboBox, &option, QStyle::SC_ComboBoxEditField, this); comboWidth = comboRect.width(); //we store the width for the delegate option.popupRect.setWidth(comboWidth);// I hoped that this would set size, no influence option.currentText = painter.fontMetrics().elidedText(option.currentText, Qt::ElideRight, comboWidth); painter.drawControl(QStyle::CE_ComboBoxLabel, option); }Attemts with showPopup()
void ComboWithElision::showPopup() { // A. Shows truncated delegates, popup field is still wide QAbstractItemView *popupView = view(); popupView->setTextElideMode(Qt::TextElideMode::ElideMiddle);//no effect popupView->setGeometry(popupView->x(), popupView->y(), 100, popupView->height()); // B. Shows truncated delegates, popup field is still wide this->view()->setMaximumWidth(100); QComboBox::showPopup(); }This is the delegate:
void DelegateClass::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (!index.isValid()) return; QStyleOptionViewItem opt = option; initStyleOption(&opt, index); opt.textElideMode = Qt::ElideRight; auto *text = &opt.text; auto *fontMetrics = &opt.fontMetrics; *text = fontMetrics->elidedText(*text, Qt::ElideRight, _parentComboWidth + 15.0); const QWidget *widget = option.widget; QStyle *style = widget ? widget->style() : QApplication::style(); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); QStyledItemDelegate::paint(painter, opt, index); }