PDA

View Full Version : Texturizing Widgets



jiveaxe
9th November 2007, 09:11
I am trying to customize the look of my application; reading the documentation I have found this (http://doc.trolltech.com/4.3/widgets-styles.html) example, in which is shown how apply texture to pushbutton; what I need is apply texture to a QListView background (instead of a flat color) and to scrollbars: which primitive element should I use? Is it possible, in a qlistview, apply different textures for the normal state and the selected one?

Regards

jpn
9th November 2007, 09:15
I'd rather use style sheets (http://doc.trolltech.com/latest/stylesheet.html) nowadays.. ;)

jiveaxe
9th November 2007, 09:31
Why? It seems simpler than the drawPrimitive() (http://doc.trolltech.com/4.3/qstyle.html#drawPrimitive) approach.

Thanks

jpn
9th November 2007, 09:35
Because using style sheets is way more simple:

http://doc.trolltech.com/4.3/stylesheet-examples.html#customizing-qabstractscrollarea
http://doc.trolltech.com/4.3/stylesheet-reference.html#background-prop

jiveaxe
9th November 2007, 09:50
Well, you are right. Writing:


listView->setStyleSheet("background-image: url(:images/notselected.png)");

is very quick.

How about the selected state for a qlistview item? Reading through properties I have found selection-background-color; how can i apply a texture also to the selected state?

Thanks

jpn
9th November 2007, 10:12
Notice that selection-background-color takes a Brush (http://doc.trolltech.com/4.3/stylesheet-reference.html#brush). Furthermore, Brush can be a PaletteRole (http://doc.trolltech.com/4.3/stylesheet-reference.html#paletterole). So you could combine QPalette and style sheets to achieve that:


QPalette palette = listView->palette();
palette.setBrush(QPalette::Highlight, QBrush(QPixmap("selection-background.png")));
listView->setPalette(palette);
listView->setStyleSheet("selection-color: palette(highlight)");

jiveaxe
9th November 2007, 11:01
I am worked on jpn's last code and this is the final implementation:


GmcListView::GmcListView(QWidget *parent)
:QListView(parent)
{
setEditTriggers(QAbstractItemView::NoEditTriggers );
QPalette palette = this->palette();
palette.setBrush(QPalette::Highlight, QBrush(QPixmap(":images/selected.png")));
palette.setBrush(QPalette::Base, QBrush(QPixmap(":images/notselected.png")));
setPalette(palette);
}

(no need to use setStyleSheet()).

Now I have customize the scrollbar look. I'd thought to create a QScrollBar widget with listView as parent thinking so to substitute the default one and working on it, but now my listView have two scrollbars...

Soon as I have a solution will post it.

jpn
9th November 2007, 11:14
Now I have customize the scrollbar look. I'd thought to create a QScrollBar widget with listView as parent thinking so to substitute the default one and working on it, but now my listView have two scrollbars...
There is no need to create new scroll bars. You can access the existing scrollbars via QAbstractScrollArea::horizontalScrollBar() and QAbstractScrollArea::verticalScrollBar(). Notice that there are quite a few "Customizing QScrollBar" examples: http://doc.trolltech.com/4.3/stylesheet-examples.html#customizing-qscrollbar

Also, you can simply use style sheet selectors (http://doc.trolltech.com/4.3/stylesheet-syntax.html#selector-types) to apply the stylesheet to a certain scrollbar. For example a QScrollBar which is child of a QListView:

QListView > QScrollBar