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. QPixmap(image) in QComboBox

QPixmap(image) in QComboBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 4.2k 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.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Is that what you are looking for ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • cpperC Offline
            cpperC Offline
            cpper
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What OS are you on ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              cpperC 1 Reply Last reply
              0
              • SGaistS SGaist

                What OS are you on ?

                cpperC Offline
                cpperC Offline
                cpper
                wrote on last edited by cpper
                #7

                @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

                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