Specifying different iconsets for the same widget



  • @ <widget class="LedButton" name="m_Standby_btn1" >

     <property name="icon" >
    
       <iconset resource="VSMgui.qrc">:/images/images/whiteCircle_nt.png</iconset>
    
     </property>
    
        <property name="text" >
    
       <string>Standby--</string>
    
     </property>
    
         <property name="visible"> <bool>false--</bool> </property>
    

    </widget>
    @
    I want to be able to display a different icon dynamically if this button is selected.
    So, I am trying to create a button with 2 png files (such that any one can be used at any point of time)>
    Is it possible to specify this in a UI file?

    Or would I have to create two buttons each with a different png file?

    Thanks

    RS



  • You can use
    @setIcon(QIcon(":/icon/SomeIcon.png"));@

    and change icon by some SIGNAL

    @setIcon(QIcon(":/icon/SomeOtherIcon.png"));@



  • Sorry i don't understood you, at first time.
    In UI file you can specify this in following way:
    @<property name="icon">
    <iconset resource="res.qrc">
    <normaloff>:/icon/Connect.png</normaloff>
    <selectedon>:/icon/Disconnect.png</selectedon>:/icon/Connect.png</iconset>
    </property>@

    or with QtCreator's interface in widgets property window(icon->Normal, Active, Selected,Disabled).



  • I'd use a style sheet for that.



  • Thanks for the inputs.

    I had tried the setIcon() solution and it works most of the time but I keep seeing the following 2 crashes once in a while and so I have decided to implement this dynamic Icon thing in the UI file now to avoid the crash.

    This is how I had used the functions
    @
    // remove explicit reference to QPixmap to avoid the crash that happens
    // sporadically
    if (bSelected) {
    setIcon(QIcon(QString::fromUtf8(":/images/images/whiteCircle_nt.png")));
    } else {
    setIcon(QIcon(QString::fromUtf8(":/images/images/blueCircle_nt.png")));
    }
    }
    @
    Please let me know if any of you have seen such intermittent crashes wen using setIcon() function.
    @
    Crash #1 Backtrace:
    #0 0x0f89a2ac in QX11Info::visual () from /usr/lib/libQtGui.so.4
    #1 0x0f8d192c in QPixmap::toImage () from /usr/lib/libQtGui.so.4
    #2 0x0fa58918 in QCommonStyle::generatedIconPixmap () from /usr/lib/libQtGui.so.4
    #3 0x0f8a3108 in QIcon::QIcon () from /usr/lib/libQtGui.so.4
    #4 0x0f8a08d8 in QIcon::pixmap () from /usr/lib/libQtGui.so.4
    @
    OR
    @
    Crash #2 Backtrace:
    #0 0x0f2f9e00 in memcpy () from /lib/tls/libc.so.6
    #1 0x10166b90 in ?? ()
    #2 0x0f521160 in QVectorData::malloc () from /usr/lib/libQtCore.so.4
    #3 0x0f937e58 in operator<< () from /usr/lib/libQtGui.so.4
    #4 0x0f96c170 in QRegion::copy () from /usr/lib/libQtGui.so.4
    #5 0x0f96a178 in QRegion::QRegion () from /usr/lib/libQtGui.so.4
    #6 0x0f96a87c in QRegion::QRegion () from /usr/lib/libQtGui.so.4
    #7 0x0f96be30 in QRegion::unite () from /usr/lib/libQtGui.so.4
    #8 0x0f9366ec in QRegion::operator+ () from /usr/lib/libQtGui.so.4
    #9 0x0f936740 in QRegion::operator+= () from /usr/lib/libQtGui.so.4
    #10 0x0f96e7a4 in QWidgetPrivate::drawWidget () from /usr/lib/libQtGui.so.4
    #11 0x0f96f734 in QWidget::update () from /usr/lib/libQtGui.so.4
    #12 0x0f96f7b8 in QWidget::update () from /usr/lib/libQtGui.so.4
    #13 0x0f96f840 in QWidget::update () from /usr/lib/libQtGui.so.4
    #14 0x0fad686c in QAbstractButton::setIcon () from /usr/lib/libQtGui.so.4
    @

    Edit: Please use @ signs around code sections; Andre



  • I tried qxoz's suggestion of adding the <normaloff> and <selectedon> tags but with this, no icon is generated. Here's how my UI file looks like: I even added a <normalon> tag just in case and that did not help either....

    @@
    <widget class="LedButton" name="m_Standby_btn" >
    <property name="icon">
    <iconset resource="VSMgui.qrc">
    <normalon>:/images/images/whiteCircle_nt.png</normalon>
    <normaloff>:/images/images/whiteCircle_nt.png</normaloff>
    <selectedon>:/images/images/blueCircle_nt.png</selectedon>
    </iconset>
    </property>
    <property name="text" >
    <string>Standby</string>
    </property>
    <property name="visible"> <bool>true</bool> </property>
    </widget>
    @@



  • I tried qxoz's suggestion of adding the <normaloff> and <selectedon> tags but with this, no icon is generated. Here's how my UI file looks like: I even added a <normalon> tag just in case and that did not help either....

    @@
    <widget class="LedButton" name="m_Standby_btn" >
    <property name="icon">
    <iconset resource="VSMgui.qrc">
    <normalon>:/images/images/whiteCircle_nt.png</normalon>
    <normaloff>:/images/images/whiteCircle_nt.png</normaloff>
    <selectedon>:/images/images/blueCircle_nt.png</selectedon>
    </iconset>
    </property>
    <property name="text" >
    <string>Standby</string>
    </property>
    <property name="visible"> <bool>true</bool> </property>
    </widget>
    @@


Log in to reply
 

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