PDA

View Full Version : QT3: mouseover effect in QListViewItem



karye
31st March 2006, 14:03
I'd like to hightlight the QListViewItem pixmap when mouse is over it.
Any one has a pointer to some code.

wysota
1st April 2006, 00:08
I don't have a pointer to any code but I can point you in the right direction. Use QListView::onItem() signal and connect it to a custom slot that will change some flag in the item so that it's drawn differently (an easiest way would be to use the setSelected() method). You'll probably have to subclass the item to implement that flag and custom painting of course. I take it that you know how to do it if you ask the question in this section...

karye
1st April 2006, 11:42
Nice!
But I need mouseOut also, or repaint all with triggerUpdate() maybe?

wysota
1st April 2006, 12:44
onItem will surely return a null pointer if you leave the view. You can act on it then by making sure none of the items are highlighted.

Something like that should (more or less) work:


class MyListView : public QListView {
Q_OBJECT
public:
MyListView ( QWidget * parent = 0, const char * name = 0, WFlags f = 0 );
private:
QListViewItem *_highlighted;
protected:
void drawContentsOffset ( QPainter * p, int ox, int oy, int cx, int cy, int cw, int ch );
private slots:
void highlightItem(QListViewItem *);
}

MyListView ( QWidget * parent, const char * name, WFlags f) : QListView ( parent, name, f ) {
_highlighted = 0;
connect(this, SIGNAL(onItem(QListViewItem*)), this, SLOT(highlightitem(QListViewItem*)));
}

void MyListView::highlightItem(QListViewItem *item){
_highlighted = item;
updateContents();
}

void MyListView::drawContentsOffset ( QPainter * p, int ox, int oy, int cx, int cy, int cw, int ch ){
// reimplement from QListView (for example copy & paste)
// there should be some loop there which iterates over items
// I assume that 'item' is a pointer to an item currently drawn


QColorGroup cg = //...
QColorGroup storecg = cg;
if(item==_highlighted){
cg.setColor(QColorGroup::Base, QColor(255,255,0)); // could be Background not Base
}
// do some stuff here probably
// eventually there will be a call like so:
item->paintCell(p, cg, ...);
// then you can restore the background
if(item==_highlighted){
cg = storecg;
}
// ...
}

Just make sure that when an item is deleted, it doesn't segfault on the _highlighted pointer (it should be ok if the code looks like the one above).