QIcon not actually generating variations for QToolButton?



  • all my "icons" start life as QImage (possibly programmatically drawn), converted to QPixMap, then to QIcon.

    so i get my QIcon, and set it into my QToolButton with toolButtonP->setIcon(pix);

    says in the doc:

    The simplest use of QIcon is to create one from a QPixmap file or resource, and then use it, allowing Qt to work out all the required icon styles

    so i thought the "pushed down" state (drawn slightly darker) (as in when the user clicks on the button) would be generated on the fly for me.

    if that's not the case, i can easily generate a darker image, but then how do i programmatically add it into QIcon as the "down state" so it's used by QToolButton?


  • Lifetime Qt Champion

    Hi
    You can add via
    https://doc.qt.io/qt-5/qicon.html#addPixmap
    for the various states.
    And yes normally it does generate for the other states when assigned in
    Designer so try add same pixmap to that role and see if it does darken it.



  • i've tried this:

    QIcon			qIcon;
    
    qIcon.addPixmap(pix, QIcon::Normal);
    
    pix.Lighten(-0.2f);	//	negative is "darken"
    // qIcon.addPixmap(pix, QIcon::Active);
    qIcon.addPixmap(pix, QIcon::Selected);
    
    toolButtonP->setIcon(qIcon);
    

    but the QToolButton still doesn't show the darkened version when the user clicks on the button. tried both Active and Selected.


  • Lifetime Qt Champion

    Hi,

    Are you sure these methods do in place modifications ? Aren't they returning a modified copy of the pixmap ?



  • i have ensured it's doing a deep copy, here is updated code:

    QIcon			qIcon;
    
    qIcon.addPixmap(pix, QIcon::Normal);
    
    CPixels			darkPix(pix);	//	deep copy, trust me
    
    darkPix.Lighten(-0.2f);	//	negative is "darken"
    qIcon.addPixmap(darkPix, QIcon::Active);
    //qIcon.addPixmap(darkPix, QIcon::Selected);
    
    toolButtonP->setIcon(qIcon);

  • Lifetime Qt Champion

    I'm not sure I am following you on that one. How did you ensure of the deep copy ?
    But you also didn't answer my question: do these two methods do in place modification ?



  • @sgaist cuz that's what my constructor does. it does this:

    CPixels::CPixels(const CPixels& other)
    {
    	*this = other;
    
    	#if _QT_
    	{
    		QImage&		self(*this);
    
    		self = other.copy();
    	}
    	#endif
    }
    

    if you're asking if "Lighten()" does in-place mod, then yes. what other method are you referring to?



  • @mrjj it's not assigned in Designer, it's assigned in code



  • note that "state" doesn't seem to be different between "off" and "on", so what is its purpose?

    image states


  • Lifetime Qt Champion

    Hi
    on/off is used for Checkable buttons.
    pr default its shown via the frame when checked but could
    also have another icon. like big fat checkmark :)

    Adding
    icon.addFile(QStringLiteral(":/test.svg"), QSize(), QIcon::Normal, QIcon::Off);
    also generates the other icons as i can later paint with them.
    (in paintevent)
    icon().paint(&p,rect(),Qt::AlignCenter, QIcon::Mode::Disabled, QIcon::State::Off);



  • i'm using QToolButton, and QImages which have already been created. I'm not loading QIcons directly from files. How do i create a QIcon that i can then load into QToolButton, such that when the user clicks it, it shows a "darkened" or "pressed" image?



  • does nobody else create QIcons programmatically, to be used in QToolButton?


 

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