Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Using icons with QTreeWidgetItem

    General and Desktop
    3
    6
    12477
    Loading More Posts
    • 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.
    • K
      Klondike6 last edited by

      I have an application using a QTreeWidget with tree nodes (QTreeWidgetItem). I would like to add icons to the nodes in my tree. I can get this to work using a file:
      [code]
      item->setIcon(0, *(new QIcon("test1.gif")));
      [/code]

      I have a resource file where I have added all the images I want to use for icons. How can I set the icon by referencing the resource file rather than directly referencing the image files?

      Thanks for any guidance.

      1 Reply Last reply Reply Quote 0
      • M
        msx_br last edited by

        Try to replace "test1.gif" with ":/icons/test1.gif".

        Look at the your resource file, the suggested path above must
        look like "<file>icons/test1.gif</file>" inside your resource file.

        msx_br - Brazil (Netherlands)

        1 Reply Last reply Reply Quote 0
        • K
          Klondike6 last edited by

          For some reason I can't get this to work.

          My resource file contains:
          [code]
          <file>Resources/cirsqr/test1.gif</file>
          [/code]

          To be more complete, here is a larger section of the resource file:
          [code]
          <qresource prefix="/cirsqr">
          <file>Resources/cirsqr/cir gray - sq gray.gif</file>
          <file>Resources/cirsqr/cir gray - sq green.gif</file>
          <file>Resources/cirsqr/cir gray - sq red.gif</file>
          <file>Resources/cirsqr/cir gray - sq yellow.gif</file>
          <file>Resources/cirsqr/cir green - sq gray.gif</file>
          <file>Resources/cirsqr/cir green - sq green.gif</file>
          <file>Resources/cirsqr/cir green - sq red.gif</file>
          <file>Resources/cirsqr/cir green - sq yellow.gif</file>
          <file>Resources/cirsqr/cir red - sq gray.gif</file>
          <file>Resources/cirsqr/cir red - sq green.gif</file>
          <file>Resources/cirsqr/cir red - sq red.gif</file>
          <file>Resources/cirsqr/cir red - sq yellow.gif</file>
          <file>Resources/cirsqr/cir yellow - sq gray.gif</file>
          <file>Resources/cirsqr/cir yellow - sq green.gif</file>
          <file>Resources/cirsqr/cir yellow - sq red.gif</file>
          <file>Resources/cirsqr/cir yellow - sq yellow.gif</file>
          <file>Resources/cirsqr/test2.gif</file>
          <file>Resources/cirsqr/test1.gif</file>
          </qresource>
          [/code]

          My code tries to reference this as follows:
          [code]
          item->setIcon(0, *(new QIcon(":/Resources/cirsqr/test1.gif")));
          [/code]

          The icon is not displayed, which indicates it is not finding the test1.gif image. Do you see anything that I am doing wrong here?

          Thanks again.

          1 Reply Last reply Reply Quote 0
          • M
            msx_br last edited by

            @
            <RCC>
            <qresource prefix="/Resources/cirsqr">
            <file>test1.gif</file>
            </qresource>
            </RCC>
            @

            On my dialog:

            @
            Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog)
            {
            ui->setupUi(this);
            ui->pushButton->setIcon(QIcon(":Resources/cirsqr/test1.gif"));
            }
            @

            Here is a screenshot:

            https://picasaweb.google.com/lh/photo/Ba7cJDkearL4oTOlnIipqdMTjNZETYmyPJy0liipFm0?feat=directlink

            msx_br - Brazil (Netherlands)

            1 Reply Last reply Reply Quote 0
            • K
              Klondike6 last edited by

              OK, I think I've got it now, I was misunderstanding how the "prefix" was used. My original resource file can work if I reference it like this:
              [code]
              item->setIcon(0, *(new QIcon(":cirsqr/Resources/cirsqr/test1.gif")));
              [/code]

              Thanks for the help!

              1 Reply Last reply Reply Quote 0
              • G
                goetz last edited by

                You're creating a memory leak. QIcon can be used as a value class, you should not create the icons with new.

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post