How to add icon to multi button in simple way?



  • 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?


  • Qt Champions 2016

    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?



  • 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
    
    
    


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



  • @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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.