Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Set QComboBox popup's size
Forum Updated to NodeBB v4.3 + New Features

Set QComboBox popup's size

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 250 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MykolaQtM Offline
    MykolaQtM Offline
    MykolaQt
    wrote on last edited by MykolaQt
    #1

    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?

    Знімок екрана 2021-10-19 111425.png

    1 Reply Last reply
    0
    • MykolaQtM Offline
      MykolaQtM Offline
      MykolaQt
      wrote on last edited by
      #2

      [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);
      }
      
      1 Reply Last reply
      0
      • MykolaQtM Offline
        MykolaQtM Offline
        MykolaQt
        wrote on last edited by
        #3

        I found the solution!

        void ComboWithElision::showPopup()
        {
            view()->window()->setFixedWidth(100);
            QComboBox::showPopup();
        }
        

        so the solution is view()->window() instead of view()

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved