Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

increase size of the images in QComboBox



  • Hello,
    I want to create a simple combo box from images only and leave some space between images after some searches I found this code which using a class that inherits from QListView this my code:
    .h

    class ComboView : public QListView
    {
    public:
        ComboView();
        struct Delegate: public QStyledItemDelegate {
            Delegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {
    
            }
            void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                auto o = option;
                initStyleOption(&o, index);
                o.decorationSize.setWidth(o.rect.width());
                o.decorationSize.setHeight(40);
                auto style =  o.widget ? o.widget->style() : QApplication::style();
                style->drawControl(QStyle::CE_ItemViewItem, &o, painter, o.widget);
            }
        };
    };
    

    .cpp

    ComboView::ComboView()
    {
        this->setSpacing(5);
        this->setItemDelegate(new Delegate);
    }
    
    

    i use it before i add the items to my combo box

        ui->comboBox->setView(new ComboView);
        ui->comboBox->addItem(QIcon(":/logos/files/bing.png"), "");
        ui->comboBox->addItem(QIcon(":/logos/files/yahoo.png"), "");
    

    this make my icons which I add to the combo box shown in the center and no text at all, the problem is that the image in the combo box is not showing completely even after I leave some space in the constructor of the ComboView class it shows the missing part is from the top and the bottom
    so how can the icon fit in its item place?


  • Lifetime Qt Champion

    Hi,

    Which size are your images ?


Log in to reply