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. [solved] QPushButton and setIcon doesn't work - icon is not showing up
Forum Updated to NodeBB v4.3 + New Features

[solved] QPushButton and setIcon doesn't work - icon is not showing up

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 8.9k 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.
  • N Offline
    N Offline
    never_ever
    wrote on last edited by
    #1

    (Visual 2012, Qt 5.2.1 - add-in)

    Hi,
    I have my class Button that inherits QPushButton. I create, using that class, a lot of buttons and I want to set icon to that buttons.
    Firstly, I wrote:

    button.cpp

    @
    Button::Button(const QString& name))
    {
    setText(name);
    setLayoutDirection(Qt::RightToLeft);
    QIcon buttonIcon(":Resources/btn.png");
    setIcon(buttonIcon);
    }
    @

    But that code doesn't work. Something is setting to that buttons because text is moving to the left but there is nothing visible (if I comment code that is responsible for setting icon, text is align to center).
    Any idea how to fix it?

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      :Resources/btn.png is a wrong path.
      What is in your resource file?

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JvdGlind
        wrote on last edited by
        #3

        Like qxoz said, you most likely forgot the '/' before "Resources".

        Jeffrey VAN DE GLIND
        Principle Consultant @ Nalys
        www.nalys-group.com

        1 Reply Last reply
        0
        • N Offline
          N Offline
          never_ever
          wrote on last edited by
          #4

          hmm.. but it still doesn't work.
          I tried couple of combinations with the same .png file and I add it to the resource.

          • I copied image to the resource folder in my project and I use relative path. - not working
          • I copied image to the resource folder in my project and I use full path - not working
          • I copied image on the desktop and I use full path - works.

          First method may not working because I have folders names with space (ie. "c:\users\me\documents\visual studio 2012\projects\project1...")
          Third method works, but I can't do it in that way.
          Second method - I have no idea why it doesn't work

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on last edited by
            #5

            Do you have a resource file (.qrc)?
            "Qt Resource System":http://qt-project.org/doc/qt-5/resources.html

            1 Reply Last reply
            0
            • N Offline
              N Offline
              never_ever
              wrote on last edited by
              #6

              Oh, sorry, my mistake. I didn't noticed that I have only .rc file (created by VS while creting project) not .qrc. Now it works.

              C 1 Reply Last reply
              0
              • N never_ever

                Oh, sorry, my mistake. I didn't noticed that I have only .rc file (created by VS while creting project) not .qrc. Now it works.

                C Offline
                C Offline
                ChimRuoi
                wrote on last edited by
                #7

                @never_ever Can you say more about this?
                0_1541245787208_aec49cdd-0ec5-41f8-8cf1-3ab0ebf189f1-image.png

                mrjjM 1 Reply Last reply
                0
                • C ChimRuoi

                  @never_ever Can you say more about this?
                  0_1541245787208_aec49cdd-0ec5-41f8-8cf1-3ab0ebf189f1-image.png

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @ChimRuoi
                  Hi and welcome to the forums
                  Its all explained here
                  http://doc.qt.io/qt-5/resources.html
                  If you have other questions, please expand a bit on what your are asking then .

                  1 Reply Last reply
                  2

                  • Login

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