Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, May 2007
item selection in model views?


Message 1 in thread

I am sorry, but it is pretty hard to understand which part of the model view 
has to be used for what. When I insert a new row in the tree, and I want this 
new item to be selected, which part has to be used? The tree view, the 
(standard) model or maybe the selection model? 

And why is the index 0, when I insert data with the following code:
	insertRow(0);
	setData(index(0, 0), ...

It works, but I dont understand why... can I use this index(0,0) as return 
value to give it to any setSelection-function?

--
 [ signature omitted ] 

Message 2 in thread

Hi,

I just looked at QItemSelectionModel documentation, there is a select() 
function.

The documentation is here: 
http://doc.trolltech.com/4.3/qitemselectionmodel.html#select

Your QTreeView object has a QItemSelectionModel (by default)..or you can 
set your own using setSelectionModel().
 From what I understand, a rough idea would be:

-- insert new row in the tree
-- get your selection model from the tree (using selectionModel())
-- select the exact row you just inserted, since you already have the 
corresponding QModelIndex
-- the selection flag you need is QItemSelectionModel::Select, 
documentation is here: 
http://doc.trolltech.com/4.3/qitemselectionmodel.html#SelectionFlag-enum

Hope this helps,
K.

J. Preiss wrote:
> I am sorry, but it is pretty hard to understand which part of the model view 
> has to be used for what. When I insert a new row in the tree, and I want this 
> new item to be selected, which part has to be used? The tree view, the 
> (standard) model or maybe the selection model? 
>
> And why is the index 0, when I insert data with the following code:
> 	insertRow(0);
> 	setData(index(0, 0), ...
>
> It works, but I dont understand why... can I use this index(0,0) as return 
> value to give it to any setSelection-function?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>   

--
 [ signature omitted ] 

Message 3 in thread

Ok, by problem was obviously, that I did not register my new selection model 
to the view. Or something like that. 
Anyway, somehow I cannot use it, and I have no idea what's going on. As 
already mentioned, I add the items (from the book "programming with qt4"):
	insertRow(0);
	setData(index(0, 0), note.getId(), Qt::UserRole);
	setData(index(0, 0), note.isIntern(), Qt::CheckStateRole);
	setData(index(0, 0), note.getId()+1);
	...
Then I catch the signal of the changed item:
	void TMainView::onJrnItemChanged(const QModelIndex& idx)
	{
		...
		if( !idx.isValid() )
			return;
		QStandardItem* item = m_Model->item(idx.row());
	qDebug() << item->text();
		int id = item->data(Qt::UserRole).toInt();
	qDebug() << "1:" << idx.row();
	qDebug() << "2: " << id;
		...
	}

and the debug says:

"378"
1: 0
2:  377
"377"
1: 1
2:  376

and I always see "the opposite" of the current element, count()-idx.row() or 
something like that. But why? Even if I insert the rows from bottom up, the 
item of idx.row() should be the current item, shouldn't it?

--
 [ signature omitted ]