View Full Version : Highlight text in list box

27th March 2007, 14:02

I want to Hightlight the text in the list box when i receive a particular message.
i want to scroll the list box without using the up/ down arrow or without clicking on the list box . this is to be done when i get a message.

when i get a message which says Next , then i need to scroll up.

how do i do this??

how do i make a default selection in the List Box???

plz help, i need this urgent....

27th March 2007, 15:27
QListBox::setCurrentItem () (http://doc.trolltech.com/3.3/qlistbox.html#setCurrentItem)

28th March 2007, 04:58
I am using QListView . will setCurrentItem() work for it?

28th March 2007, 09:37
Since you wrote 'list box' I thought you meant Qt3.
Since QListView doesn't have setCurrentItem() it wont work.
In Qt4 the model-view approach is used much more then in Qt3.
I didn't have a chance to wrok with it yet under Qt4 - but from what I have read so far, i think you should use the selction model.
QItemSelectionModel::select ()

And this might be helpful as well: http://doc.trolltech.com/4.2/model-view-selection.html

29th March 2007, 06:22
I did that the following way:

QListViewItem *current = listview->currentItem();

QListViewItem *after = current->itemAbove(); OR itemBelow()

this way it is working.....i am able to highlight the next item in the list whenever i get a particular signal.

7th April 2007, 21:09
I guess my question fits this thread, but solution given in last post I think isn't apropriate for Qt4.
So, what exactly I want to do. I have a widget with QListView, QDirModel and QSelectionModel and actually I want the widget to remember activated directory, so when I come back cursor/highlighting will be on that directory.
we have following items in current dir:
. .. pics walls sample.png pict.jpg
What is default: Key_Down x 3 /*go to pics*/, Key_Return, ... , <go to "..">, enter, Key_Down x 4 /*go to walls*/, enter.
What I want: Key_Down x 3 /*go to pics*/, enter, ..., <go to "..">, enter, Key_Down x 1 /*go to walls*/, enter.

setCurrentIndex() doesn't work, it sets but doesn't highlight that. And you can then press Key_{Return,Enter} (btw are they equal? I know that they have different values, but anyway...) and activate it, but if you press Key_Down you will be at "."

How to solve the problem? I don't need the full code for this =) but just wanna know how to set position of List View cursor.

How to get rid of "." item, but keep ".."? I know about QDir::NoDotAndDotDot, but what about QDir::NoDot ?

Ok, found this in QListWidget::setItemSelected(const QListWidgetItem *item, bool select)

if (d->selectionMode == SingleSelection) {
selectionModel()->select(index, select
? QItemSelectionModel::ClearAndSelect
: QItemSelectionModel::Deselect);

But when I come into /home/eruart/imgs code:

selmodel->select(dirmodel->index("/home/eruart/imgs/example.png"), QItemSelectionModel::ClearAndSelect);
gives me following in console:
intersectingStaticSet: row 11 was invalid
intersectingStaticSet: row 12 was invalid

What does it mean?
update 2

static QModelIndex last;
if (!dirmodel->fileName(current).contains("..")) last = current;
qDebug() << last;

current here is const QModelIndex & that passed by listview's activated signal, and this code is handleActivated slot that connected with the signal.

QModelIndex(4,0,0x80984a8,QDirModel(0x8082aa8) )
intersectingStaticSet: row 11 was invalid
intersectingStaticSet: row 12 was invalid

~/imgs $ ls -a | wc
11 12 135

Why rows 11 and 12, but not 4?

8th April 2007, 08:55
And now I am completely confused. I have removed all selecting code and those messages with intersectingStaticSet continue to appear.

Hm, I recompiled with Qt4.3beta (was 4.2.3) and those messages disappeared.

But selecting still doesn't work.

9th April 2007, 11:24
I'll answer by myself, in case if someone will need it. It's far from ideal, but works for me.

unsigned int lastrow;

QStringList nameFilters;
nameFilters << "??*" << ".." ;// FIX ME: one char length names doesn't match
void kivBrowser::handleActivated(const QModelIndex & current)
if (dirmodel->isDir(current))

if (!current.row()) // FIX ME: if we cd up but never was there we would select wrong item or fail to select at all.
QModelIndex last = listview->rootIndex().child(lastrow,0);
else lastrow = current.row();

emit dirChanged(dirmodel->filePath(current));
emit imgChanged(dirmodel->filePath(current));