February 20, 2012

vivek.narvekar vivek.narvek..
Lab Rat
37 posts

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

6 replies

February 20, 2012

Andre Andre
Robot Herder
6417 posts

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

February 20, 2012

vivek.narvekar vivek.narvek..
Lab Rat
37 posts

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.

February 20, 2012

Andre Andre
Robot Herder
6417 posts

In that case, perhaps a QProxyStyle can be of use to you?

February 20, 2012

Mabrouk Mabrouk
Lab Rat
21 posts

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

February 20, 2012

vivek.narvekar vivek.narvek..
Lab Rat
37 posts

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

May 22, 2012

vivek.narvekar vivek.narvek..
Lab Rat
37 posts

Hello Andre and Mabrouk,

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

  1. if(!icon ().isNull ()){
  2.   initStyleOption(&option);
  3.   option.text = QString(""); // set the empty string so that only icon is drawn and not the text, when we specify both icon and text.
  4.   if(m_mouseOverButton == true){
  5.    option.icon = icon().pixmap ( iconSize(), QIcon::Active);
  6.   }else{
  7.    option.icon = icon().pixmap ( iconSize(), QIcon::Normal);
  8.   }
  9.   QPainter painter(this);
  10.   style()->drawControl(QStyle::CE_PushButton, &option, &painter, this);
  11.   //event->ignore ();
  12.  }else{
  13.   QPushButton::paintEvent (event);
  14.  }

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…

 
  ‹‹ How Can I use Hardware Rendering instead of Software Rendering with QtSDK 4.8.0 on Mac OS X Lion?      is it possible to split a particular collumn into subcolumn in table widget? ››

You must log in to post a reply. Not a member yet? Register here!