QPushButton with an alpha level of 0% turns solid when moused over. How to fix Qt with C++ (with stylesheets?)

StackOverflow https://stackoverflow.com/questions/14512682

質問

I have designed two different kinds of QPushButtons in Qt Designer. One is the default, the other I want to be clear. In QT Designer I set the latter's initial style sheet like such:

background-color: rgba( 35, 35, 35, 0% );

I want the button to have a border and to be clear (the internal color isn't really important; that just happens to match the color scheme). When I mouse over the button I want it to REMAIN clear, however instead it turns to the solid grey that is 35/35/35.

When the button receives a signal press I want it to turn a half transparent green:

    background-color: rgba( 0, 255, 0, 50% );

Before I realized the mouse-over would be a problem, I had this coded:

QString MyPanel::FillInvisibleButton(bool activeButton)
{
  QString invisibleButton;

  if (activeButton) {
     invisibleButton = "QPushButton{background-color: rgba(0, 255, 0, 50%);}";
  } else {
     invisibleButton = "QPushButton{background-color: rgba(35, 35, 35, 0%);}";
  }
  return invisibleButton;
}


//Signals
void MyPanel::MyButtonPressed()
{
   m_ui->myButton->setStyleSheet(FillInvisibleButton(true));
}

void MyPanel::MyButtonReleased()
{
   m_ui->myButton->setStyleSheet(FillInvisibleButton(false));
}

That works great as far as making the clear button a transparent green when it's pressed... but it has the big grey opaque block on it when it's moused over which I want to go away!

I've tried this:

    QString MyPanel::FillInvisibleButton(bool activeButton)
{
  QString invisibleButton;

  if (activeButton) {
     invisibleButton = "QPushButton{background-color: rgba(0, 255, 0, 50%);} QPushButton:hover{background-color: rgba(35, 35, 35, 0%);}";
  } else {
     invisibleButton = "QPushButton{background-color: rgba(35, 35, 35, 0%);} QPushButton:hover{background-color: rgba(35, 35, 35, 0%);}";
  }
  return invisibleButton;
}

And it doesn't work at all. They still mouse-over grey and now they don't turn green at all.

Is my syntax just wrong? Or?? I'm new to Qt and very rusty on C++. I'm not married to the Stylesheets. If there's a better way to accomplish it that's great. Just keep in mind that I can't do this to ALL of my buttons; just several of them.

Thanks!

ETA: I believe I'm using 4.5.0

役に立ちましたか?

解決

You don't need to use signals, stylesheet is enough. Try this one:

QPushButton {
    background-color: rgba( 35, 35, 35, 0% );
    border: 1px solid black;
}

QPushButton:pressed {
    background-color: rgba( 0, 255, 0, 50% );
}

他のヒント

In your stylesheet you can use 'hover' pseudo state:

button->setStyleSheet("QPushButton:hover { ... }");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top