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

QPixmap(image) in QComboBox



  • The documentation on QComboBox says "Comboboxes can contain pixmaps as well as strings". I suppose this doesn't refer to icons.
    However, I can't see any member function to do that, at least not in Qt 5. Is the documentation outdated? Is there any way to do this ?
    Meanwhile I'll experiment with stylesheets.


  • Lifetime Qt Champion

    Hi,

    You can use this overload of addItem to which you can pass an icon.

    Is that what you are looking for ?



  • Nope, I want to fill the entire cell/item with a pixmap.


  • Lifetime Qt Champion

    You can set the decoration role of your items with the pixmap you want to show.

    Or depending on how you would like to render your images, you can also use a custom delegate that does what you want.



  • @SGaist said in QPixmap(image) in QComboBox:

    You can set the decoration role of your items with the pixmap you want to show.

    I tried this:

     QPixmap p1(":/res/grad0-120.jpeg");
     QPixmap p2(":/res/grad0-240.jpeg");
     QPixmap p3(":/res/grad240-360.jpeg");
    
     ui->combo->setItemData(0,p1,Qt::DecorationRole);
     ui->combo->setItemData(1,p2,Qt::DecorationRole);
     ui->combo->setItemData(2,p3,Qt::DecorationRole);
    

    and got this:

    0_1557094365824_2d774fb0-11b0-445a-90b9-dea0196715d3-image.png

    As you can see, the pixmaps display fine in the dropdown, but are very small in the top cell. Any idea how to solve this ? I have some CSS applied to the combo box, but it's not causing any problem. Also it appears there is an annoying 5px white strip on the left side of the items.


  • Lifetime Qt Champion

    What OS are you on ?



  • @SGaist
    Windows 7

    I managed to also do it differently, setting the images as icons to each combo element, and the doing some CSS adjustments, and

    ui->mapColorMapComboBox->setIconSize(QSize(65,20));
    

    It's far from perfect, but it's better:

    0_1557363474334_ddcaf4a7-a14c-43f4-9dba-8ff58db9677d-image.png


Log in to reply