PDA

View Full Version : QStyle button hover



Apocalypse
30th March 2008, 12:08
How can i make QPushButton look like hovered by mouse using QStyle? Maybe Style Flags or something else?

jpn
30th March 2008, 12:41
I recommend taking a look at sources when you want to find these things out (QPushButton::initStyleOption() or something similar in your case). It's a state flag passed in QStyleOption.

Apocalypse
30th March 2008, 13:51
I know that i can do it by using QStyle::State :


void form1:;paintEvent(QPaintEvent*) {
QStyleOptionButton sob;
sob.initFrom(this);
QPainter painter(this);
sob.rect=QRect(10,10,50,50);
sob.state=QStyle::State(QStyle::State_MouseOver);
style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);
}


But this given't result, button still look like State_Enabled. Maybe i did something wrong?

jpn
30th March 2008, 14:08
They are flags. You should combine a set of flags by using OR operator:


sob.state |= QStyle::State_MouseOver;

Notice what QStyleOption::initFrom() docs say.. It already initializes state so basically you overrode it with plain QStyle::State_MouseOver.

Apocalypse
30th March 2008, 15:05
One more question. What flag can make that button look like Disabled?

jpn
30th March 2008, 16:45
So remove QStyle::State_Enabled which has been turned on by QStyleOption::initFrom(). It's all about simple bitwise operations (http://en.wikipedia.org/wiki/Bitwise_operation) :)


sob.state &= ~QStyle::State_Enabled;

Apocalypse
30th March 2008, 17:39
It did nothing. Maybe i must delete some more flags?

jpn
30th March 2008, 17:51
It did nothing. Maybe i must delete some more flags?
Are you sure? Which style is that? With some styles it's just hard to notice the difference when there's no text:


sob.text = "Button";

Apocalypse
30th March 2008, 19:26
No, i'm using Oxygen style and ~QStyle::State_Enabled doesn't works for me. Maybe i must do something else?

jpn
30th March 2008, 19:55
Try with other style to verify it works? Well, at least it works perfectly for me. :)

Apocalypse
31st March 2008, 10:32
It doesn't works with other styles. Do you know another variants?

jpn
31st March 2008, 11:06
Could you show the current code of yous and explain what does "does not work" mean in your case? :)

Apocalypse
31st March 2008, 12:02
Sure. It means that ~QStyle::State_Enabled given't result and button still look like enabled. To make button disabled i use :


QStyleOptionButton sob;
QPainter painter(this);
sob.initFrom(this);
sob.rect=QRect(5,55, 93,22);
sob.state = QStyle::State_Sunken;
style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);
sob.rect=QRect(5,80, 93,22);
sob.state &= ~QStyle::State_Enabled & ~QStyle::State_Sunken;
style()->drawControl(QStyle::CE_PushButton, &sob, &painter, this);


~QStyle::State_Sunken works and button become enabled. But i want to make it disabled. I tried to delete initFrom, but it didn't worked too;

jpn
31st March 2008, 12:26
Could you do me a favor: compile the following code and run it with "-style motif" arguments.



// main.cpp
#include <QtGui>

class Widget : public QWidget
{
public:
void paintEvent(QPaintEvent*)
{
QPainter painter(this);

QStyleOptionButton opt;
opt.state = 0;
opt.text = "Disabled";
opt.rect = QRect(0,0,100,25);
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);

opt.state = QStyle::State_Enabled;
opt.text = "Enabled";
opt.rect = QRect(100,0,100,25);
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);

opt.state = QStyle::State_Sunken;
opt.text = "Disabled,Sunken";
opt.rect = QRect(0,25,100,25);
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);

opt.state = QStyle::State_Enabled|QStyle::State_Sunken;
opt.text = "Enabled,Sunken";
opt.rect = QRect(100,25,100,25);
style()->drawControl(QStyle::CE_PushButton, &opt, &painter, this);
}
};

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

Apocalypse
31st March 2008, 13:20
Yes, you are right. The button looks like disabled in motif and other squared styles.
But i need another effect. To explain what i want i made next addition into your code:


Widget()
{
QPushButton *button = new QPushButton(this);
button->move(0,50);
button->setDisabled(true);
button->setText("Disabled PushButton");
button->show();
this->resize(200,200);
}


And i made 4 screensots off app in few styles. You can find it in attachment. Could you tell me how can i draw my button same as "Disabled PushButton"?

jpn
31st March 2008, 13:40
Try setting the palette to use "disabled" colors:


// sets the palette of this
opt.initFrom(this);
// "this" is not disabled so we switch the color group by hand
opt.palette.setCurrentColorGroup(QPalette::Disable d);

Apocalypse
31st March 2008, 14:19
That's it! Thank you!