What QICon state is used when QToolButton isDown?



  • Hello,
    I am successfully using multiple QIcon states for various states of QToolButton. However, I cannot see how to use a different state when a button "isDown". I have done web searches and I see this discussed from time to time but have not yet found a direct answer on how to do this.


  • Lifetime Qt Champion

    Hi,

    Doesn't the On/Off state do that ?


  • Moderators

    @buckler
    I think this is not supported by default.

    Try the following example:

    #define COLOREDPIX(COLOR) QPixmap COLOR ## Pix(16,16); COLOR ## Pix.fill(Qt:: ## COLOR);
    ...
    COLOREDPIX(red);
    COLOREDPIX(yellow);
    COLOREDPIX(green);
    COLOREDPIX(blue);
    COLOREDPIX(black);
    COLOREDPIX(white);
    COLOREDPIX(magenta);
    COLOREDPIX(cyan);
    
    QIcon icon;
        icon.addPixmap(redPix, QIcon::Normal, QIcon::On);
        icon.addPixmap(yellowPix, QIcon::Disabled, QIcon::On);
        icon.addPixmap(greenPix, QIcon::Active, QIcon::On);
        icon.addPixmap(bluePix, QIcon::Selected, QIcon::On);
        icon.addPixmap(blackPix, QIcon::Normal, QIcon::Off);
        icon.addPixmap(whitePix, QIcon::Disabled, QIcon::Off);
        icon.addPixmap(magentaPix, QIcon::Active, QIcon::Off);
        icon.addPixmap(cyanPix, QIcon::Selected, QIcon::Off);
    
    QPushButton* button = new QPushButton;
        button->setIcon( icon );
        button->show();
    

    QIcon::Normal -> unfocused
    QIcon::Disabled -> disabled
    QIcon::Selected -> selected (maybe used in QGraphicsView, i don't know)
    QIcon::Active -> focused

    QIcon::On -> checked
    QIcon::Off -> unchecked

    A possible workaround - when you do not depend on the checked state of the button:

    void MyPushButton::paintEvent(QPaintEvent *)
    {
        QStylePainter p(this);
        QStyleOptionButton option;
        initStyleOption(&option);
        if( m_isPressed )
              option.state |= QStyle::State_On;
        p.drawControl(QStyle::CE_PushButton, option);
    }
    

    Make sure you set the m_isPressed in the mouseButtonPress event handler to true and to false in the mouseButtonRelease event handler.
    This is also necessary for keyPress/Release for the space key.



  • Thank you both! Raven-worx confirms that while it isn't easy to do, yet it is possible to do!


Log in to reply
 

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