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. Using icons with QTreeWidgetItem

Using icons with QTreeWidgetItem

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 14.3k Views
  • 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 Offline
    K Offline
    Klondike6
    wrote on 11 Mar 2012, 20:59 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      msx_br
      wrote on 11 Mar 2012, 21:23 last edited by
      #2

      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
      0
      • K Offline
        K Offline
        Klondike6
        wrote on 11 Mar 2012, 22:47 last edited by
        #3

        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
        0
        • M Offline
          M Offline
          msx_br
          wrote on 11 Mar 2012, 23:45 last edited by
          #4

          @
          <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
          0
          • K Offline
            K Offline
            Klondike6
            wrote on 12 Mar 2012, 00:14 last edited by
            #5

            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
            0
            • G Offline
              G Offline
              goetz
              wrote on 12 Mar 2012, 22:16 last edited by
              #6

              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
              0

              5/6

              12 Mar 2012, 00:14

              • Login

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