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. Adding Icon on top of Text in a QPushButton
Forum Updated to NodeBB v4.3 + New Features

Adding Icon on top of Text in a QPushButton

Scheduled Pinned Locked Moved General and Desktop
qpushbuttonicontop
2 Posts 2 Posters 3.6k 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.
  • P Offline
    P Offline
    Pranjal Jain
    wrote on last edited by Pranjal Jain
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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