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 png as Icon?
Forum Updated to NodeBB v4.3 + New Features

How to add png as Icon?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 2.4k Views
  • 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.
  • S Offline
    S Offline
    SRaD
    wrote on last edited by
    #1

    I am trying to get a button with an icon show up in my toolbar, so I have the following.

    const QIcon editIcon = QIcon("~/downloads/edit-icon.png");
    
    QAction *editAction = new QAction(editIcon, tr("Edit Vehicle"), this);
    editAction->setStatusTip(tr("Edit Vehicle."));
    vehicleToolbar->addAction(editAction);
    
    connect(editAction, &QAction::triggered, this, &MainWindow::editVehicle);
    

    The button is there (though invisible), and if I click on it, it works, but ... there's no icon on the button.

    Thoughts?

    KillerSmathK 1 Reply Last reply
    0
    • S SRaD

      I am trying to get a button with an icon show up in my toolbar, so I have the following.

      const QIcon editIcon = QIcon("~/downloads/edit-icon.png");
      
      QAction *editAction = new QAction(editIcon, tr("Edit Vehicle"), this);
      editAction->setStatusTip(tr("Edit Vehicle."));
      vehicleToolbar->addAction(editAction);
      
      connect(editAction, &QAction::triggered, this, &MainWindow::editVehicle);
      

      The button is there (though invisible), and if I click on it, it works, but ... there's no icon on the button.

      Thoughts?

      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by KillerSmath
      #2

      @SRaD
      Are you sure if ~/downloads/edit-icon.png is a valid path ?

      If your intention is access the icon by home folder of your system, you can use QDir::homePath()

      #include <QDir>
      
      QDir::homePath() + "/downloads/edit-icon.png"
      

      Note: Files (icons,txt,etc) can be embedded in your application using Qt Resource System.

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      7
      • Pradeep P NP Offline
        Pradeep P NP Offline
        Pradeep P N
        wrote on last edited by
        #3

        @SRaD
        As @KillerSmath said, the image may not be accessible from the code due to path issue.

        Please check the Qt Resource file system and it is better to use it the contents remains with the project code.

            QAction *editAction = new QAction(QIcon(":/imgAddButtonPressed74x74.png"), tr(""), this);
        
        

        Adding image in Resources qrc file

        RESOURCES += \
            image.qrc
        
        <RCC>
            <qresource prefix="/">
                <file>imgAddButtonPressed74x74.png</file>
            </qresource>
        </RCC>
        

        Pradeep Nimbalkar.
        Upvote the answer(s) that helped you to solve the issue...
        Keep code clean.

        1 Reply Last reply
        6
        • Pradeep P NP Offline
          Pradeep P NP Offline
          Pradeep P N
          wrote on last edited by
          #4

          Hi @SRaD

          Is the problem resolved ?
          So we can close the thread as solved.

          Pradeep Nimbalkar.
          Upvote the answer(s) that helped you to solve the issue...
          Keep code clean.

          1 Reply Last reply
          1

          • Login

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