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. Can't give button a QIcon!

Can't give button a QIcon!

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.3k 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
    nullbuil7
    wrote on last edited by
    #1

    I've tried both png and ico versions but they don't work. and this is one of the first examples of the getting started with qt5
    so i didn't write it. maybe it wasn't tested with windows 10 and mingw32 ? does anyone know how to make the button have an Icon?

    #include <QApplication>
    #include <QPushButton>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QPushButton myButton(QIcon("Save.ico"), "Push me");
        myButton.setToolTip("Click this to turn back the hands of time");
        myButton.show();
    
        return app.exec();
    }
    
    1 Reply Last reply
    0
    • N nullbuil7

      i found that the problem is path and can be resolved using absolute path but that's where the problem starts.
      as i said i just started and i can't use c-string and Qstring together to create a relative path under windows. ( i can't use experimental::filesystem either) so i don't know how to pass a relative path under windows 10 / migw32 with QString.
      any suggestions?
      i tried : //icon.ico, \icon.ico, ..//icon.ico, ..\icon.co etc.

      Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #9

      @nullbuil7 QIcon("..\\Save.ico") or QIcon("../Save.ico"). Either of those would work but don't do that. Relative paths are relative to current working directory, which can be different depending on how you start your app (via exe click, shortcut, command line etc.).
      If your path is relative to your exe location then retrieve a path of your exe and build an absolute path at runtime.

      For example, if your exe is in C:\MyApp\bin\MyApp.exe and your icon is in C:\MyApp\data\icon.ico you can get a path to it like this:

      QString  path = qApp->applicationDirPath() + "/../data/icon.ico";
      

      also, take a look at Qt Resources System. You can just embed your icon inside the data segent of your exe and use a resource path like this QIcon(":/icon.ico").

      1 Reply Last reply
      5
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Are you sure your app can find that icon, i.e. QIcon("Save.ico").isNull() doesn't return true? With a path like that the ico file needs to be in the same directory as the exe of your app.

        1 Reply Last reply
        4
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #3

          Passing a reference to a temporary (stack) object? Unless you can be certain that the framework copies what's needed from the QIcon it will not exist after the call to the constructor, right?

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          JonBJ Chris KawaC 2 Replies Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            Passing a reference to a temporary (stack) object? Unless you can be certain that the framework copies what's needed from the QIcon it will not exist after the call to the constructor, right?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Kent-Dorfman
            Correct me if I'm wrong: if you don't think QPushButton keeps a copy/increments the count of any QIcon passed to it, then everywhere you pass an icon to a button you'd have to keep the icon in scope for as long as you use the button, which doesn't sound right?

            1 Reply Last reply
            2
            • Kent-DorfmanK Kent-Dorfman

              Passing a reference to a temporary (stack) object? Unless you can be certain that the framework copies what's needed from the QIcon it will not exist after the call to the constructor, right?

              Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @Kent-Dorfman said:

              Unless you can be certain that the framework copies what's needed

              Good APIs don't store pointers to parameters passed by const & and Qt doesn't here. That would be a disaster waiting to happen. The icon is copied and it's a shallow copy since QIcon is one of the implicitly shared classes.

              The code is fine. It's either a missing resource, wrong path or missing image format plugin.

              1 Reply Last reply
              2
              • N Offline
                N Offline
                nullbuil7
                wrote on last edited by
                #6

                i found that the problem is path and can be resolved using absolute path but that's where the problem starts.
                as i said i just started and i can't use c-string and Qstring together to create a relative path under windows. ( i can't use experimental::filesystem either) so i don't know how to pass a relative path under windows 10 / migw32 with QString.
                any suggestions?
                i tried : //icon.ico, \icon.ico, ..//icon.ico, ..\icon.co etc.

                JonBJ Chris KawaC 2 Replies Last reply
                0
                • N nullbuil7

                  i found that the problem is path and can be resolved using absolute path but that's where the problem starts.
                  as i said i just started and i can't use c-string and Qstring together to create a relative path under windows. ( i can't use experimental::filesystem either) so i don't know how to pass a relative path under windows 10 / migw32 with QString.
                  any suggestions?
                  i tried : //icon.ico, \icon.ico, ..//icon.ico, ..\icon.co etc.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @nullbuil7 said in Can't give button a QIcon!:

                  as i said i just started and i can't use c-string and Qstring together to create a relative path under windows.

                  Sorry, but this doesn't make sense.

                  It sounds like you would be best making an absolute path at run-time, so that you don't have to rely on what the current directory might or might not be if you use a relative path. You can build the path using whatever Qt call/variable it is which gives you, say, the path to the executable, if that's where your icon file lives. Or, maybe you should embed the icon as a resource?

                  1 Reply Last reply
                  3
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    What @JonB is suggesting is e.g. to use QCoreApplication::applicationDirPath()

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    4
                    • N nullbuil7

                      i found that the problem is path and can be resolved using absolute path but that's where the problem starts.
                      as i said i just started and i can't use c-string and Qstring together to create a relative path under windows. ( i can't use experimental::filesystem either) so i don't know how to pass a relative path under windows 10 / migw32 with QString.
                      any suggestions?
                      i tried : //icon.ico, \icon.ico, ..//icon.ico, ..\icon.co etc.

                      Chris KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on last edited by Chris Kawa
                      #9

                      @nullbuil7 QIcon("..\\Save.ico") or QIcon("../Save.ico"). Either of those would work but don't do that. Relative paths are relative to current working directory, which can be different depending on how you start your app (via exe click, shortcut, command line etc.).
                      If your path is relative to your exe location then retrieve a path of your exe and build an absolute path at runtime.

                      For example, if your exe is in C:\MyApp\bin\MyApp.exe and your icon is in C:\MyApp\data\icon.ico you can get a path to it like this:

                      QString  path = qApp->applicationDirPath() + "/../data/icon.ico";
                      

                      also, take a look at Qt Resources System. You can just embed your icon inside the data segent of your exe and use a resource path like this QIcon(":/icon.ico").

                      1 Reply Last reply
                      5

                      • Login

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