Show only Icon for push button without text[solved]



  • Hello,

    When I set icon to a push button and also set text using setText, It shows text as well as icon.

    Is there a way, by which, I can show only the icon (when it is set), otherwise text, when icon is not set? this was happening automatically in Qt3..... I am trying to port Qt3 code to Qt 4......

    Thanks for any help

    Regards
    Vivek



  • I think (but please be sure to check) that that is the behaviour of [[doc:QToolButton]]. At least, it provides a greater level of control over this than QPushButton.



  • Thanks a lot Andre for responding.

    I was hoping that there would be some style setting or something in pushbutton, that would solve this. Now the problem is that we have used QPushButton extensively, hence it wont be a straight forward task to replace QpushButton with QtoolButton.

    anyways, thanks a lot for your valuable suggestion.



  • In that case, perhaps a [[doc:QProxyStyle]] can be of use to you?



  • a work around for this is just set the icon size to the button size and don't set a text for the button and you'll get the result you want



  • Thanks Andre and Mabrouk

    Andre - I am looking into QProxyStyle, and see if it can be of use, in this case

    Mabrouk - eliminating text setting is a difficult task here, because of the code architecture.

    Edit: removed @ characters, as the forum software interprets those as tags surrounding code sections; Andre



  • Hello Andre and Mabrouk,

    I managed to solve this problem by using QStyleOptionButton, as follows

    @if(!icon ().isNull ()){
    QStyleOptionButton option;
    initStyleOption(&option);
    option.text = QString(""); // set the empty string so that only icon is drawn and not the text, when we specify both icon and text.
    if(m_mouseOverButton == true){
    option.icon = icon().pixmap ( iconSize(), QIcon::Active);
    }else{
    option.icon = icon().pixmap ( iconSize(), QIcon::Normal);
    }
    QPainter painter(this);
    style()->drawControl(QStyle::CE_PushButton, &option, &painter, this);
    //event->ignore ();
    }else{
    QPushButton::paintEvent (event);
    }@

    Thus when the icon is present, set the option text to empty string. This way, the original string is preserved.

    Thanks a lot for all the help...


Log in to reply
 

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