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. QFontComboBox custom item height
Forum Updated to NodeBB v4.3 + New Features

QFontComboBox custom item height

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 316 Views 2 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.
  • W Offline
    W Offline
    wazzdaman
    wrote on last edited by
    #1

    Hello,

    I'd like to customize QFontComboBox to have bigger item height but somehow the formatting gets lost in the process.

    Default:
    default.png

    Custom style:
    styled.png

    My code:

    class DropDownItemDelegate : public QStyledItemDelegate
    {
    public:
         using QStyledItemDelegate::QStyledItemDelegate;
        
         QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override
         {
             QSize s = QStyledItemDelegate::sizeHint(option, index);
             s.setHeight(40);
             return s;
         }   
    };  
     
    ui->fontComboBox->view()->setItemDelegate(new DropDownItemDelegate(ui->fontComboBox));
    ui->fontComboBox->setEditable(false);
    

    Environment: Win 10, Qt5.15.2

    Any tips?

    Thanks a lot!

    Gojir4G 1 Reply Last reply
    0
    • W wazzdaman

      Hello,

      I'd like to customize QFontComboBox to have bigger item height but somehow the formatting gets lost in the process.

      Default:
      default.png

      Custom style:
      styled.png

      My code:

      class DropDownItemDelegate : public QStyledItemDelegate
      {
      public:
           using QStyledItemDelegate::QStyledItemDelegate;
          
           QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override
           {
               QSize s = QStyledItemDelegate::sizeHint(option, index);
               s.setHeight(40);
               return s;
           }   
      };  
       
      ui->fontComboBox->view()->setItemDelegate(new DropDownItemDelegate(ui->fontComboBox));
      ui->fontComboBox->setEditable(false);
      

      Environment: Win 10, Qt5.15.2

      Any tips?

      Thanks a lot!

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @wazzdaman said in QFontComboBox custom item height:

      QFontComboBox

      QFontComboBox use a dedicated item delegate internally, called QFontFamilyDelegate. So by setting a new delegate you are breaking the visual from the original delegate. Unfortunately the class QFontFamilyDelegate cannot be included as it is defined in the cpp of QFontComboBox. You could probably copy and update it according to your needs but instead of that, I think there is a much simpler solution using style sheet, which seems to work according to your needs. Of course you may need to play a bit with the values for having the result you want.

      ui->fontComboBox->setFixedHeight(40)
      ui->fontComboBox->setStyleSheet("QFontComboBox { font-size: 25px }")
      
      M W 2 Replies Last reply
      1
      • Gojir4G Gojir4

        @wazzdaman said in QFontComboBox custom item height:

        QFontComboBox

        QFontComboBox use a dedicated item delegate internally, called QFontFamilyDelegate. So by setting a new delegate you are breaking the visual from the original delegate. Unfortunately the class QFontFamilyDelegate cannot be included as it is defined in the cpp of QFontComboBox. You could probably copy and update it according to your needs but instead of that, I think there is a much simpler solution using style sheet, which seems to work according to your needs. Of course you may need to play a bit with the values for having the result you want.

        ui->fontComboBox->setFixedHeight(40)
        ui->fontComboBox->setStyleSheet("QFontComboBox { font-size: 25px }")
        
        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        Some kind of hack ...

        auto listView=qobject_cast<QListView*>(combo->view());
        if(listView) listView->setSpacing(6);
        
        1 Reply Last reply
        1
        • Gojir4G Gojir4

          @wazzdaman said in QFontComboBox custom item height:

          QFontComboBox

          QFontComboBox use a dedicated item delegate internally, called QFontFamilyDelegate. So by setting a new delegate you are breaking the visual from the original delegate. Unfortunately the class QFontFamilyDelegate cannot be included as it is defined in the cpp of QFontComboBox. You could probably copy and update it according to your needs but instead of that, I think there is a much simpler solution using style sheet, which seems to work according to your needs. Of course you may need to play a bit with the values for having the result you want.

          ui->fontComboBox->setFixedHeight(40)
          ui->fontComboBox->setStyleSheet("QFontComboBox { font-size: 25px }")
          
          W Offline
          W Offline
          wazzdaman
          wrote on last edited by
          #4

          @Gojir4 @mpergand

          Thanks for the replies! It seems none of your proposed solutions kept the styling or changed just the height of items, maybe it's not possible at all without significant amount of work :(

          1 Reply Last reply
          0

          • Login

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