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 add icon to multi button in simple way?
Forum Update on Tuesday, May 27th 2025

How to add icon to multi button in simple way?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qpixmapqicon
5 Posts 3 Posters 2.1k 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.
  • MhM93M Offline
    MhM93M Offline
    MhM93
    wrote on last edited by
    #1

    Hi. I have a lot of button that I want to set icon to them .
    I do this just for one of them:

    QString str=(qApp->applicationDirPath());
        str.append("/pic/kb.png");
        QPixmap pixmap(str);
        QIcon ButtonIcon(pixmap);
        ui->btnShowKB->setIcon(ButtonIcon);
        ui->btnShowKB->setIconSize(pixmap.rect().size());
    

    but I have a lot of button (btn1,btn2,btn3,....,btn9)
    How can I set the pix map with another image user(/pic/1.png , /pic/2.png , /pic/3.png , .... , /pic/9.png)
    Should I make new QPixmap for each one?

    H.Ghassami

    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      You can easy find all buttons

      List<QPushButton *> ButtonList = findChildren<QPushButton*>();
      

      So easy to add same picture to all of them.

      But its not clear what u want ?
      U want same picture for all or you want to use different for each?

      What does this image show?

      MhM93M 1 Reply Last reply
      0
      • R Offline
        R Offline
        Rohith
        wrote on last edited by
        #3

        Hi MhM93,

        I have tried this way in my code in order to solve my requirement,here in my case i want to create multiple items.
        I hope this may help you

        QStringList pathlist,namelist;
            pathlist<<":/images/audio.png"<<":/images/bash.png"<<":/images/bluetooth.png"<<":/images/battery.png"<<":/images/camera.png"
                   <<":/images/ethernet.png"<<":/images/fingerprint.png";
         namelist<<"AUDIO"<<"BASH"<<"BLUETOOTH"<<"BATTERY"<<"CAMERA"<<"ETHERNET"<<"FP-SCANNER";
        QString name,path;
        
            for(int i=0; i<ChildCount1; i++)
            {
                QString IO;
                if(IO == "AUDIO"){
                    count=0;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
        
                if(IO == "BASH"){
                    count=1;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
        
                if(IO == "BLUETOOTH"){
                    count=2;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
        
                if(IO == "BATTERY"){
                    count=3;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
        
                if(IO == "CAMERA"){
                    count=4;
                   path = pathlist.at(count);
                   name = namelist.at(count);
        }
        
                if(IO == "ETHERNET"){
                    count=5;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
        
                if(IO == "FP-SCANNER"){
                    count=6;
                   path = pathlist.at(count);
                   name = namelist.at(count);
                }
                QListWidgetItem *CommonItem = new QListWidgetItem(contentsWidget);
                CommonItem->setIcon(QIcon(path));
                CommonItem->setText(name);
                CommonItem->setTextAlignment(Qt::AlignHCenter);
                path.clear();
                name.clear();
            }
        
        Try this it may guide you.
        
        Thanks & Regards,
        Rohith.G
        
        
        
        MhM93M 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          You can easy find all buttons

          List<QPushButton *> ButtonList = findChildren<QPushButton*>();
          

          So easy to add same picture to all of them.

          But its not clear what u want ?
          U want same picture for all or you want to use different for each?

          What does this image show?

          MhM93M Offline
          MhM93M Offline
          MhM93
          wrote on last edited by
          #4

          @mrjj said:

          Hi
          You can easy find all buttons

          List<QPushButton *> ButtonList = findChildren<QPushButton*>();
          

          So easy to add same picture to all of them.

          But its not clear what u want ?
          U want same picture for all or you want to use different for each?

          What does this image show?

          I have a keyboard in my application like calculator. each button has an image ( forexample button 1 has an image that the number one on this image. and the the image of the other numbers.)

          H.Ghassami

          1 Reply Last reply
          0
          • R Rohith

            Hi MhM93,

            I have tried this way in my code in order to solve my requirement,here in my case i want to create multiple items.
            I hope this may help you

            QStringList pathlist,namelist;
                pathlist<<":/images/audio.png"<<":/images/bash.png"<<":/images/bluetooth.png"<<":/images/battery.png"<<":/images/camera.png"
                       <<":/images/ethernet.png"<<":/images/fingerprint.png";
             namelist<<"AUDIO"<<"BASH"<<"BLUETOOTH"<<"BATTERY"<<"CAMERA"<<"ETHERNET"<<"FP-SCANNER";
            QString name,path;
            
                for(int i=0; i<ChildCount1; i++)
                {
                    QString IO;
                    if(IO == "AUDIO"){
                        count=0;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
            
                    if(IO == "BASH"){
                        count=1;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
            
                    if(IO == "BLUETOOTH"){
                        count=2;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
            
                    if(IO == "BATTERY"){
                        count=3;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
            
                    if(IO == "CAMERA"){
                        count=4;
                       path = pathlist.at(count);
                       name = namelist.at(count);
            }
            
                    if(IO == "ETHERNET"){
                        count=5;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
            
                    if(IO == "FP-SCANNER"){
                        count=6;
                       path = pathlist.at(count);
                       name = namelist.at(count);
                    }
                    QListWidgetItem *CommonItem = new QListWidgetItem(contentsWidget);
                    CommonItem->setIcon(QIcon(path));
                    CommonItem->setText(name);
                    CommonItem->setTextAlignment(Qt::AlignHCenter);
                    path.clear();
                    name.clear();
                }
            
            Try this it may guide you.
            
            Thanks & Regards,
            Rohith.G
            
            
            
            MhM93M Offline
            MhM93M Offline
            MhM93
            wrote on last edited by MhM93
            #5

            @Rohith : thanks.but your solution does not work for me. I have multi button. I for example want to set image (/pic/1.png) to btn1 and then set another image to another button.
            My question How to set image to them in my code behind?

            H.Ghassami

            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