Defining Path



  • I need to update different images(stored in different path) on the same PushButton at different events.How can I do this without using pustbutton->setIcon(:/pathoftheimage/image1) for the second time as pustbutton->setIcon(:/pathoftheimage2/image2) and so on.
    Please help.

    Thanks,
    Revu



  • It depens what event means to you.

    You can add different pixmaps to QIcon using QIcon::addPixmap() or QIcon::addFile() for different modes (normal, disabled, active, selected) and states (on, off) which are then automatically displayed when the QPushButton enters a specific state and mode.

    If an event is something specific to your application there is no way around using QPushButton::setIcon(). If you don't want to have different pathes all over you code you could for example

    • use a central icon pool
      @
      class YourClass
      {
      ...

    public:
    enum Icon
    {
    StateAIcon,
    StateBIcon
    };

    QIcon icon(YourClass::Icon icon)
    {
        return ...;
    }
    
    ...
    
    void stateAEvent()
    {
        pushButton->setIcon(icon(YourClass::StateAIcon));
    }
    

    };
    @

    • use resource aliases
      @
      class YourClass
      {
      ...

    public:
    void stateAEvent()
    {
    pushButton->setIcon(QIcon(":/icons/stateA.png"));
    }
    };

    <RCC>
    <qresource prefix="/icons">
    <file alias="stateA.png">someFolder/someFile.png</file>
    <file alias="stateB.png">differentFolder/subFolder/someFile.png</file>
    ...
    </qresource>
    </RCC>
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.