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. How to create custom delegate for QComboBox
Servers for Qt installer are currently down

How to create custom delegate for QComboBox

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 271 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.
  • O Offline
    O Offline
    Oleksandr_Radi
    wrote 28 days ago last edited by
    #1

    Hello, I am trying to create delegate for QComboBox that displays only image without text.
    I have tried to create custom delegate, but I don't understand how to do it at all.
    paint() is callled only when QComboBox is expanded.
    Can someone please help me and explain how does delegates work?

    Sorry for my bad english.

    P 1 Reply Last reply 28 days ago
    0
    • O Oleksandr_Radi
      28 days ago

      Hello, I am trying to create delegate for QComboBox that displays only image without text.
      I have tried to create custom delegate, but I don't understand how to do it at all.
      paint() is callled only when QComboBox is expanded.
      Can someone please help me and explain how does delegates work?

      Sorry for my bad english.

      P Offline
      P Offline
      Pl45m4
      wrote 28 days ago last edited by
      #2

      @Oleksandr_Radi

      What is your goal?

      Delegates work as editor widgets. The delegates activate as you open the drop-down menu.

      • https://doc.qt.io/qt-6/qcombobox.html#setItemDelegate

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zvoopz
        wrote 27 days ago last edited by
        #3

        Вітаю, пане Олександр
        To display images in QComboBox items you can use QIcon class with the addItem method

                ui->customComboBox->addItem(QIcon(":/img/img0.png"), "Item 0");
                ui->customComboBox->addItem(QIcon(":/img/img1.png"), "Item 1");
                ui->customComboBox->addItem(QIcon(":/img/img2.png"), "Item 2");
                ui->customComboBox->addItem(QIcon(":/img/img3.png"), "Item 3");
        

        The QIcon(":/img/imgX.png") syntax assumes you are using .qrc file.

        If you just want images and no text:

        ui->customComboBox->addItem(QIcon(":/img/img1.png"), "");
        
        O 1 Reply Last reply 27 days ago
        1
        • Z zvoopz
          27 days ago

          Вітаю, пане Олександр
          To display images in QComboBox items you can use QIcon class with the addItem method

                  ui->customComboBox->addItem(QIcon(":/img/img0.png"), "Item 0");
                  ui->customComboBox->addItem(QIcon(":/img/img1.png"), "Item 1");
                  ui->customComboBox->addItem(QIcon(":/img/img2.png"), "Item 2");
                  ui->customComboBox->addItem(QIcon(":/img/img3.png"), "Item 3");
          

          The QIcon(":/img/imgX.png") syntax assumes you are using .qrc file.

          If you just want images and no text:

          ui->customComboBox->addItem(QIcon(":/img/img1.png"), "");
          
          O Offline
          O Offline
          Oleksandr_Radi
          wrote 27 days ago last edited by Oleksandr_Radi
          #4

          @zvoopz I am creating icon selector, after image selection I need to write path to that image to database. I want to store path to image in QComboBox, but do not display that path. I think to achieve it I need to subclass QComboBox, does it is?

          P 1 Reply Last reply 27 days ago
          0
          • O Oleksandr_Radi
            27 days ago

            @zvoopz I am creating icon selector, after image selection I need to write path to that image to database. I want to store path to image in QComboBox, but do not display that path. I think to achieve it I need to subclass QComboBox, does it is?

            P Offline
            P Offline
            Pl45m4
            wrote 27 days ago last edited by
            #5

            @Oleksandr_Radi

            Maybe some external model where you map the icon path to the actual image and a widget mapper might be a better idea.

            • https://doc.qt.io/qt-6/qdatawidgetmapper.html#details

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            O 1 Reply Last reply 26 days ago
            1
            • Z Offline
              Z Offline
              zvoopz
              wrote 27 days ago last edited by
              #6

              You can make

              enum imageID {
              Image_None = 0,
              //large enum values to insert additional items in list if you further would need to
              //icon of www
              img0	=10001,
              img1    = 10002,
              img2    = 10003,
              
              //icon of conflict
              warning = 12001,
              error = 12002,
              info = 12003,
              
              //other icons
              img3     = 14001,
              img4   = 14002,
              img5    = 14003,
              };
              

              Assign items to enum

              ui->customComboBox->addItem(QIcon(":/img/img0.png"), QVariant(static_cast<int>(img0));
              ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1));
              ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning));
              ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));
              
              void MainWindow::on_customComboBox_activated(){
              int customIndex = ui->customComboBox->currentData().toInt();
              QString pathToImage;
              switch(customIndex){
              
              case 10001:{
              //img0 selected
              //pathToImage = specified_path
              }
              }
              
              O 1 Reply Last reply 26 days ago
              0
              • Z zvoopz
                27 days ago

                You can make

                enum imageID {
                Image_None = 0,
                //large enum values to insert additional items in list if you further would need to
                //icon of www
                img0	=10001,
                img1    = 10002,
                img2    = 10003,
                
                //icon of conflict
                warning = 12001,
                error = 12002,
                info = 12003,
                
                //other icons
                img3     = 14001,
                img4   = 14002,
                img5    = 14003,
                };
                

                Assign items to enum

                ui->customComboBox->addItem(QIcon(":/img/img0.png"), QVariant(static_cast<int>(img0));
                ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1));
                ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning));
                ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));
                
                void MainWindow::on_customComboBox_activated(){
                int customIndex = ui->customComboBox->currentData().toInt();
                QString pathToImage;
                switch(customIndex){
                
                case 10001:{
                //img0 selected
                //pathToImage = specified_path
                }
                }
                
                O Offline
                O Offline
                Oleksandr_Radi
                wrote 26 days ago last edited by
                #7

                @zvoopz said in How to create custom delegate for QComboBox:

                ui->customComboBox->addItem(QIcon(":/img/img1.png"), QVariant(static_cast<int>(img1));
                ui->customComboBox->addItem(QIcon(":/img/warning.png"), QVariant(static_cast<int>(warning));
                ui->customComboBox->addItem(QIcon(":/img/error.png"), QVariant(static_cast<int>(error));

                Thank you for your response, but I am chosing @Pl45m4 method, because my program may load icons dynamically. But anyway, thank you for help!

                1 Reply Last reply
                0
                • P Pl45m4
                  27 days ago

                  @Oleksandr_Radi

                  Maybe some external model where you map the icon path to the actual image and a widget mapper might be a better idea.

                  • https://doc.qt.io/qt-6/qdatawidgetmapper.html#details
                  O Offline
                  O Offline
                  Oleksandr_Radi
                  wrote 26 days ago last edited by
                  #8

                  @Pl45m4 Your solution is well suited for my purpose! Thank you!

                  1 Reply Last reply
                  0
                  • O Oleksandr_Radi has marked this topic as solved 26 days ago

                  8/8

                  15 May 2025, 18:14

                  • Login

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