[Qt-interest] QStyle vs. style sheets
Ben Axelrod
baxelrod at coroware.com
Fri Jul 10 00:34:06 CEST 2009
Thanks Oliver,
I had not thought about CSS being parsed at runtime. That doesn't sound desirable for my application. Also, since I am programmatically extracting the images, I don't think CSS will like that very much. So I decided to give the QStyle a try, and I think I came up with a pretty good solution.
I made a button class that I will use for all my buttons. It has a method to let me load in a set of images, and in the overridden paint event, it simply decides which image to show, then calls style()->drawItemPixmap().
Code below:
MyPushButton::MyPushButton(QWidget* parent) : QPushButton(parent)
{
setCheckable(true);
}
void MyPushButton::paintEvent(QPaintEvent* event)
{
QStyleOptionButton option;
option.initFrom(this);
bool hover = option.state & QStyle::State_MouseOver;
QPixmap image;
if (!isEnabled())
{
image = *disabled;
}
else
{
if (isChecked())
{
if (isDown())
{
image = *onClick;
}
else if (hover)
{
image = *onHover;
}
else
{
image = *on;
}
}
else
{
if (isDown())
{
image = *offClick;
}
else if (hover)
{
image = *offHover;
}
else
{
image = *off;
}
}
}
QPainter painter(this);
QRect rect = QRect(0, 0, image.width(), image.height());
setFixedWidth(image.width());
setFixedHeight(image.height());
style()->drawItemPixmap(&painter, rect, Qt::AlignLeft | Qt::AlignTop, image);
}
void MyPushButton::setImages(QPixmap* on,
QPixmap* onHover,
QPixmap* onClick,
QPixmap* off,
QPixmap* offHover,
QPixmap* offClick,
QPixmap* disabled)
{
this->on = on;
this->onHover = onHover;
this->onClick = onClick;
this->off = off;
this->offHover = offHover;
this->offClick = offClick;
this->disabled = disabled;
setFixedWidth(on->width());
setFixedHeight(on->height());
}
-----Original Message-----
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Oliver.Knoll at comit.ch
Sent: Thursday, July 09, 2009 3:25 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] QStyle vs. style sheets
Ben Axelrod wrote on Thursday, July 09, 2009 4:14 AM:
> I am using Qt to try to match the look and feel of an existing
> application. The buttons in the app are very unique looking, and I
> have images for the various button states. I am wondering if doing
> it through QStyle or style sheets would be better.
I have never done "GUI pimping" myself, but from what I've read in the Qt docs (and heard at one of the Qt DevDays :) I think using Style Sheets (CSS) is good for "simple cases", e.g. changing the background pixmap of widgets, providing button state images etc.), but if you have lot of these widgets, it might not be as performant as using QStyleSheet.
Think of CSS as "being parsed at runtime" (maybe over and over again for each dialog popping up) and QStyleSheet as being "natively compiled" (after all, it is C++ code you are writing there).
QStyleSheets over you much more flexibility, but are somewhat more complicated to understand and more work is to be done, I guess.
As I said, I have used neither technique, but I would go for the CSS approach first. If it overs you the funtionality you need and you don't notice any performance loss in the GUI, then go for it. Otherwise implement your button states using QStyleSheets (I think for merely customising buttons QStyleSheets are not that hard, either - and I am sure there are examples in your QTDIR/exampels folder somewhere ;)
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
_______________________________________________
Qt-interest mailing list
Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list