Trolltech Home | Qt4-preview-feedback Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt4-preview-feedback Archive, February 2008
Problem with flat Property of QPushButton


Message 1 in thread

Hi,

The flat attribute remains not true as I've initialized it.

I've a push button with a styleSheet on it which has 2 different background images. One for the pressed an one for the up state.
To see only the background image on the button, the button is flat.
In the up state state of the button everything works fine. But in the pressed state the button appears not flat anymore and therefore
I can't see my background image anymore. In the paintEvent I observed, that the isFlat() function does not return true as it should.

Here's the code of my derived push button:
class Cc_PushButtonSpeechUnit : public QPushButton
{
  Q_OBJECT
public:
  Cc_PushButtonSpeechUnit(QWidget* parent = 0):
  QPushButton(parent){setFlat(true);};
  virtual ~Cc_PushButtonSpeechUnit(){};
  void Cc_PushButtonSpeechUnit::paintEvent(QPaintEvent*)
  {
    QStylePainter painter(this);
    painter.drawControl(QStyle::CE_PushButton, getStyleOption());
    .... do something specific ...
  };
  QStyleOptionButton Cc_PushButtonSpeechUnit::getStyleOption() const
  {
    QStyleOptionButton opt;
    opt.initFrom(this);
    if(isFlat())
       opt.features |= QStyleOptionButton::Flat;
    if(menu())
       opt.features |= QStyleOptionButton::HasMenu;
    if(isDown() || (menu() && menu()->isVisible()))
       opt.state |= QStyle::State_Sunken;
    if(isChecked())
       opt.state |= QStyle::State_On;
    if(!isDown())
        opt.state |= QStyle::State_Raised;
    opt.icon = icon();
    opt.iconSize = iconSize();
    return opt;
  };
};

Here's the code snipet of where I set the style sheet:
    QString styleString = QString(
      "QPushButton {\n"
      "  border-width: 0px;\n"
      "  background-image: url(%1);\n"
      "  background-repeat: no-repeat;\n"
      "  background-position: top right;\n"
      "}\n"
      "QPushButton:pressed {\n"
      "  margin-left: 2px;\n"
      "  margin-top: 2px;\n"
      "  background-image: url(%2);\n"
      "  background-repeat: no-repeat;\n"
      "}")
      .arg(metadata->getSpeechUnitButtonPushButtonBackgroundImagePath(imageNormal))
      .arg(metadata->getSpeechUnitButtonPushButtonBackgroundImagePath(imagePressed));
    SpeechUnitPushButton->setStyleSheet(styleString);

I assume that the painter uses the flat attribute to paint as expected.
It worked in Qt 4.2.2.

Cheers
Rainer Buschow
_______________________________________________________________________
Siemens Schweiz AG, Engineering and Innovative Products, EIP E61
Rainer Buschow, Software Entwickler
Albisriederstrasse 245, CH-8047 Zürich
Telefon +41 (0) 585 584 032, Fax +41 (0) 585 543 330 
mailto:rainer.buschow@xxxxxxxxxxx, Internet: http://www.siemens.ch/

Wichtiger Hinweis: Diese E-Mail und allfällige Anlagen können firmenvertrauliche Informationen enthalten. Sollten Sie diese E-Mail irrtümlich erhalten haben, benachrichtigen Sie uns bitte durch Antwort-Mail und löschen Sie diese E-Mail nebst Anlagen von Ihrem System. Vielen Dank.

Important note: This e-mail and any attachment may contain trade secrets. If you have received it in error, please notify us by reply e-mail and then delete this e-mail and any attachment from your system. Thank you for your cooperation.