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. QIcon, how to set it from code?
Forum Updated to NodeBB v4.3 + New Features

QIcon, how to set it from code?

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

    Hi everyone,

    I want to create GUI application for basic text file manipulation. My application up to now has 4 simple QPushButtons: load, save, save-as and exit. My goal is to set up default icons on them. I use Linux Mint 17.

    What is a real problem: When I go in QtDesigner, click on pushbutton I can simply write under "Theme" bar: "document-open" and it will load Icon for document open. However when I put in my code next line:

    QIcon saveAs = QIcon::fromTheme("document-save-as");
    ui->SaveAsFileBtn->setIcon(saveAs);

    it does not change icon. I assume that the problem is my theme. Can anyone please help me how to set up these Icons.

    thank you in advance

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

      Hi and welcome to devnet,

      Are you sure your theme provides such an icon ?

      You can check with the isNull method.

      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
      • VrtniPatuljakV Offline
        VrtniPatuljakV Offline
        VrtniPatuljak
        wrote on last edited by VrtniPatuljak
        #3

        Thank you on warm welcome,

        You were correct it is null but I am confused now a lot. What bothers me is next:

        For instance:
        In Qt Designer I drag QPushButton and open his properties. Next I navigate to Icon property and start writing in tab "Theme" : document-open or document-save and some kind of icon is showing in tab icon. My question is how to use that icon?

        My english is not the best-but I hope you guys know what I mean.

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

          It might be that the current theme you are using doesn't provide an icon for "document-save-as"

          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
          • VrtniPatuljakV VrtniPatuljak

            Hi everyone,

            I want to create GUI application for basic text file manipulation. My application up to now has 4 simple QPushButtons: load, save, save-as and exit. My goal is to set up default icons on them. I use Linux Mint 17.

            What is a real problem: When I go in QtDesigner, click on pushbutton I can simply write under "Theme" bar: "document-open" and it will load Icon for document open. However when I put in my code next line:

            QIcon saveAs = QIcon::fromTheme("document-save-as");
            ui->SaveAsFileBtn->setIcon(saveAs);

            it does not change icon. I assume that the problem is my theme. Can anyone please help me how to set up these Icons.

            thank you in advance

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @VrtniPatuljak
            maybe it helps setting the theme explicitly using QIcon::setThemeName("THEME-NAME").

            QtDoc:

            The name should correspond to a directory name in the themeSearchPath() containing an index.theme file describing it's contents.

            Check the search paths on your system and see what they actually contain.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • VrtniPatuljakV Offline
              VrtniPatuljakV Offline
              VrtniPatuljak
              wrote on last edited by VrtniPatuljak
              #6

              It have "document-save-as" icon, it display it in the QT designer.

              I try also QIcon::setThemeName() but unfortunately it did not give any positive results. QIcon::themeSearchPath() gives me "/usr/share/icons/" directory, but there are many themes inside :/ I don't know how to choose correct one. Is there a way to find what theme is currently active on my desktop?

              1 Reply Last reply
              0
              • VrtniPatuljakV Offline
                VrtniPatuljakV Offline
                VrtniPatuljak
                wrote on last edited by VrtniPatuljak
                #7

                Ok, so here how I did it.

                I literally search for icon path, first of all I have found what theme my OS is currently using. Then I found where are my Icons stored, after that I have found my icon.

                
                std::string search = QIcon::themeSearchPaths()[0].toStdString();
                std::string out = GetStdoutFromCommand("gsettings list-recursively | grep icon-theme ");
                std::vector <int> positions;
                for (int i = 0; i < out.size();++i)
                        if (out[i] == '\'') positions.push_back(i);
                
                int p1 = positions[positions.size()-1], p2 = positions[positions.size()-2];
                p2=p2+1;
                std::string theme = out.substr(p2,p1-p2);
                search +="/";
                std::string path;
                
                path = "find "+search+theme+"/* -iname document-open.png | grep '32'";
                std::string icon_open = GetStdoutFromCommand(path);
                

                Many parsing is done but I think that generally idea is ok. And also here is the code to capture bash command output: GetStdoutFromCommand

                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