Adding Icon on top of Text in a QPushButton



  • Hi,
    Is there a way to put an icon on top of text in a QPushButton. I know there is a way in a QToolButton, but I want to use QPushButton (constraint). Please tell me how to do so, even if it requires to overwrite the paintEvent.

    I found a way using stylesheet, but that takes away in-built features of that button (on hover, click etc) and its very difficult to maintain the consistency with various styles.

            char iconName[80];
            strcat(iconName, ":/");
            strcat(iconName, this->iconButton());
            QPixmap icon(iconName);
            int iconHeight = icon.height(); 
    
            QString styleSheet = "background: url(:/%1) top center no-repeat; padding-top: %2px;";
            this->setStyleSheet(styleSheet.arg(this->iconButton()).arg(iconHeight));
            this->setIcon(QIcon());
    

    But this stylesheet takes away all the style of the button. I want to retain the style. Please suggest ways.

    Thanks!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Why not just call setIcon with your current icon ? No need for a style sheet


Log in to reply
 

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