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 Updated to NodeBB v4.3 + New Features

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.
  • M Offline
    M Offline
    MhM93
    wrote on 11 May 2016, 09:44 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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 11 May 2016, 09:52 last edited by mrjj 5 Nov 2016, 09:52
      #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?

      M 1 Reply Last reply 11 May 2016, 11:07
      0
      • R Offline
        R Offline
        Rohith
        wrote on 11 May 2016, 09:58 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
        
        
        
        M 1 Reply Last reply 11 May 2016, 11:07
        1
        • M mrjj
          11 May 2016, 09:52

          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?

          M Offline
          M Offline
          MhM93
          wrote on 11 May 2016, 11:07 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
            11 May 2016, 09:58

            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
            
            
            
            M Offline
            M Offline
            MhM93
            wrote on 11 May 2016, 11:07 last edited by MhM93 5 Dec 2016, 05:17
            #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

            1/5

            11 May 2016, 09:44

            • Login

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