Qt World Summit: Register Today!

QIcon, how to set it from code?

  • 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");

    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

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Are you sure your theme provides such an icon ?

    You can check with the isNull method.

  • 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.

  • Lifetime Qt Champion

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

  • Moderators

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


    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.

  • 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?

  • 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];
    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

Log in to reply