PDA

View Full Version : Highlight text in list box



Sheetal
27th March 2007, 15:02
Hi....

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....

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

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

high_flyer
28th March 2007, 10: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

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

QListViewItem *current = listview->currentItem();

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

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

Eruart
7th April 2007, 22: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.
Example:
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.

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

updated
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;
listview->selectionModel()->select(last,QItemSelectionModel::ClearAndSelect);

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?

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

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

But selecting still doesn't work.

Eruart
9th April 2007, 12: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;

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

listview->setCurrentIndex(listview->rootIndex().child(0,0));
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);
listview->setCurrentIndex(last);
}
else lastrow = current.row();

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