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 being reutilized during creation of QAction, is that possible?
Forum Updated to NodeBB v4.3 + New Features

QIcon being reutilized during creation of QAction, is that possible?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 376 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.
  • L Offline
    L Offline
    leonardoMB
    wrote on last edited by leonardoMB
    #1

    The following code is part of my constructor

    iconTitle = QIcon(":/a/a.png");
    actionTitle = QString(tr("a"));
    _ActionOne = new QAction(iconTitle, actionTitle, this);
    iconTitle = QIcon(":/a/b.png");
    actionTitle = QString(tr("b"));
    _ActionTwo = new QAction(iconTitle, actionTitle, this);
    

    I would like to know Why the code above works. I mean, the iconTitle is passed as reference and we change it. So all Actions refer to the same memory, right?

    1 Reply Last reply
    0
    • L leonardoMB

      @ChrisW67
      But isn't it an assignment operator instead of a copy constructor call?

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

      @leonardoMB
      Here's my 2 cents, based on intuition & superstition :)

      When you pass the const QIcon & to QAction it increments a reference count, for the icon, not the iconTitle variable. Changing iconTille to (effectively "point to") a different QIcon won't have any effect on, say, your QAction call.

      L 1 Reply Last reply
      0
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #2

        You pass the QIcon by const reference and the QAction constructor makes a copy. That copy is not connected to the original after that. The same thing is happening with the title text.

        A copy is required because the QAction object has to control/ensure the lifetime of the icon it may providing for display. For example, you can call the constructor like this:

        _ActionOne = new QAction(iconTitle, QIcon(":/a/a.png"), this);
        

        where the temporary QIcon has no existence beyond this statement, but the QAction must still function in a tool bar it is added to.

        The Qt internals are designed to make such copies as efficient as they can be.

        L 1 Reply Last reply
        1
        • C ChrisW67

          You pass the QIcon by const reference and the QAction constructor makes a copy. That copy is not connected to the original after that. The same thing is happening with the title text.

          A copy is required because the QAction object has to control/ensure the lifetime of the icon it may providing for display. For example, you can call the constructor like this:

          _ActionOne = new QAction(iconTitle, QIcon(":/a/a.png"), this);
          

          where the temporary QIcon has no existence beyond this statement, but the QAction must still function in a tool bar it is added to.

          The Qt internals are designed to make such copies as efficient as they can be.

          L Offline
          L Offline
          leonardoMB
          wrote on last edited by
          #3

          @ChrisW67
          But isn't it an assignment operator instead of a copy constructor call?

          JonBJ 1 Reply Last reply
          1
          • L leonardoMB

            @ChrisW67
            But isn't it an assignment operator instead of a copy constructor call?

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

            @leonardoMB
            Here's my 2 cents, based on intuition & superstition :)

            When you pass the const QIcon & to QAction it increments a reference count, for the icon, not the iconTitle variable. Changing iconTille to (effectively "point to") a different QIcon won't have any effect on, say, your QAction call.

            L 1 Reply Last reply
            0
            • JonBJ JonB

              @leonardoMB
              Here's my 2 cents, based on intuition & superstition :)

              When you pass the const QIcon & to QAction it increments a reference count, for the icon, not the iconTitle variable. Changing iconTille to (effectively "point to") a different QIcon won't have any effect on, say, your QAction call.

              L Offline
              L Offline
              leonardoMB
              wrote on last edited by
              #5

              @JonB You are right, I understood it from the source code, right now and you reinforced it, thanks

              1 Reply Last reply
              0

              • Login

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