Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Adding Icon on top of Text in a QPushButton
Pranjal Jain last edited by Pranjal Jain
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; 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.
Hi and welcome to devnet,
Why not just call setIcon with your current icon ? No need for a style sheet