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

Qt-interest Archive, May 2007
How to set the current inde xof a list?

Pages: Prev | 1 | 2 | Next

Message 1 in thread

When I fill the data into my treeview, the treeview has no current item, but 
the first item is selected... strange.

Now I tried almost everything, to get the last item selected (or the first, if 
descending ordering works):

	loadData();
	treeJrn->scrollToBottom();
	treeJrn->setCurrentIndex(m_Proxy->index(m_Proxy->rowCount(), 0));
	
treeJrn->selectionModel()->setCurrentIndex(m_Proxy->index(m_Proxy->rowCount(), 
0), QItemSelectionModel::SelectCurrent);

It scrolls to the last item, but nothing is selected. 

Proxy is the SortFilterProxy, the model behind is a QStandardItemModel.

-----------------------------------
Forty-Three GmbH 
Neugrabenweg 5-7
66123 Saarbrücken
Fon: +49 (0)681-95814-0
Fax: +49 (0)681-95814-14
HRB 13266 Amtsgericht Saarbrücken
Geschäftsführer: Dr. Christian Gill

--
 [ signature omitted ] 

Message 2 in thread

On 23.05.07 17:54:15, J. Preiss wrote:
> When I fill the data into my treeview, the treeview has no current item, but 
> the first item is selected... strange.
> 
> Now I tried almost everything, to get the last item selected (or the first, if 
> descending ordering works):
> 
> 	loadData();
> 	treeJrn->scrollToBottom();
> 	treeJrn->setCurrentIndex(m_Proxy->index(m_Proxy->rowCount(), 0));
> 	
> treeJrn->selectionModel()->setCurrentIndex(m_Proxy->index(m_Proxy->rowCount(), 
> 0), QItemSelectionModel::SelectCurrent);
> 
> It scrolls to the last item, but nothing is selected. 

None of these methods select an item, they only set the current item.
See the api documentation for QItemSelectionModel on how to select an
index (or range of indexes) and also for the difference between current
item and selected items.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

> None of these methods select an item, they only set the current item.
> See the api documentation for QItemSelectionModel on how to select an
> index (or range of indexes) and also for the difference between current
> item and selected items.

Nice hint :-) I have 

void QAbstractItemView::setCurrentIndex ( const QModelIndex & index )   [slot]
"To set an item as the current item without selecting it, call
selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);"

Well, I used QItemSelectionModel::Select instead, so there *should* be 
something selected.

Additionally, I read
"Depending on the current selection mode, the item may also be selected."

That can only be, when I use Ctrl- or Shift-selecting, I assume...


In the meantime, I examined all combinations of <class>->setCurrentIndex with 
all QItemSelectionModel::<Flags> plus selectionModel()->select with all 
QItemSelectionModel::<Flags>. And probably I did not even manage to set the 
current item :-(

I *know* that the current item is not always the selected item. But IMHO, for 
convenience purpose, there should be a selection mode get exactly this 
behaviour.
Any maybe something like indexOfLastRow() to avoid constructs like 
m_Proxy->index(m_Proxy->rowCount(), 0)

--
 [ signature omitted ] 

Message 4 in thread

J. Preiss wrote:
> void QAbstractItemView::setCurrentIndex ( const QModelIndex & index )
[...]
> "Depending on the current selection mode, the item may also be
selected."
> 
> That can only be, when I use Ctrl- or Shift-selecting, I assume...

i think that is referring to QAbstractItemView::SingleSelection

in that mode, the selection is the same as the current item.

> In the meantime, I examined all combinations of
<class>->setCurrentIndex
> with
> all QItemSelectionModel::<Flags> plus selectionModel()->select with
all
> QItemSelectionModel::<Flags>. And probably I did not even manage to
set
> the
> current item :-(

use QItemSelectionModel::select() instead.

 
> I *know* that the current item is not always the selected item. But
IMHO,
> for
> convenience purpose, there should be a selection mode get exactly this
> behaviour.
> Any maybe something like indexOfLastRow() to avoid constructs like
> m_Proxy->index(m_Proxy->rowCount(), 0)

there is, its just in the view, not in the selectionmodel:
QAbstractItemView::SingleSelection :-)

Cheers,
Peter

--
 [ signature omitted ] 

Message 5 in thread

Well, setting 

	treeJrn->setSelectionMode(QAbstractItemView::SingleSelection);

helps a lot. After loading data, I call

	treeJrn->scrollToTop();
	treeJrn->setCurrentIndex(m_Proxy->index(0, 0));

and the first item is selected. "Unfortunately", the descending order works 
now, so the selectionChanged(index) signal is callled:

	void TMainView::onJrnItemChanged(const QModelIndex& idx)
	{
		QStandardItem* item = m_Model->item(idx.row());
		...
	}

and once again, I see the data of (count()-idx.row()) is shown. Ok, let's do 
it like in the other functions:

	QModelIndex index = m_Proxy->mapToSource(idx);
	QStandardItem* item = m_Model->item(index.row());

But this crashes as soon as the program starts in

	QModelIndex QSortFilterProxyModelPrivate::proxy_to_source(const QModelIndex 
&proxy_index) const
	{
	    if (!proxy_index.isValid())
	        return QModelIndex(); // for now; we may want to be able to set a 
root index later
	    IndexMap::const_iterator it = index_to_iterator(proxy_index);
	    Mapping *m = it.value();
		...


When I use the non-crashing function with the wrong index, the program starts, 
and I can call the delete function, which does the same:

	void TMainView::on_buttonDel_clicked()
	{
		QModelIndex idx = treeJrn->currentIndex();
		if( !idx.isValid() )
			return;
		QModelIndex index = m_Proxy->mapToSource(idx);
		QStandardItem* item = m_Model->itemFromIndex(m_Model->index(index.row(), 
0));
	}

and here it works and shows me the correct data.

(where m_Proxy the sorting model and m_Model the QStandardItemModel).



--
 [ signature omitted ] 

Message 6 in thread

what do you connect the TMainView::onJrnItemChanged to?

Cheers,
Peter

> -----Original Message-----
> From: J. Preiss [mailto:auba@xxxxxxx]
> Sent: Thursday, May 24, 2007 2:50 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: How to set the current inde xof a list?
> 
> Well, setting
> 
> 	treeJrn->setSelectionMode(QAbstractItemView::SingleSelection);
> 
> helps a lot. After loading data, I call
> 
> 	treeJrn->scrollToTop();
> 	treeJrn->setCurrentIndex(m_Proxy->index(0, 0));
> 
> and the first item is selected. "Unfortunately", the descending order
> works
> now, so the selectionChanged(index) signal is callled:
> 
> 	void TMainView::onJrnItemChanged(const QModelIndex& idx)
> 	{
> 		QStandardItem* item = m_Model->item(idx.row());
> 		...
> 	}
> 
> and once again, I see the data of (count()-idx.row()) is shown. Ok,
let's
> do
> it like in the other functions:
> 
> 	QModelIndex index = m_Proxy->mapToSource(idx);
> 	QStandardItem* item = m_Model->item(index.row());
> 
> But this crashes as soon as the program starts in
> 
> 	QModelIndex QSortFilterProxyModelPrivate::proxy_to_source(const
> QModelIndex
> &proxy_index) const
> 	{
> 	    if (!proxy_index.isValid())
> 	        return QModelIndex(); // for now; we may want to be able
to
> set a
> root index later
> 	    IndexMap::const_iterator it =
index_to_iterator(proxy_index);
> 	    Mapping *m = it.value();
> 		...
> 
> 
> When I use the non-crashing function with the wrong index, the program
> starts,
> and I can call the delete function, which does the same:
> 
> 	void TMainView::on_buttonDel_clicked()
> 	{
> 		QModelIndex idx = treeJrn->currentIndex();
> 		if( !idx.isValid() )
> 			return;
> 		QModelIndex index = m_Proxy->mapToSource(idx);
> 		QStandardItem* item = m_Model->itemFromIndex(m_Model-
> >index(index.row(),
> 0));
> 	}
> 
> and here it works and shows me the correct data.
> 
> (where m_Proxy the sorting model and m_Model the QStandardItemModel).
> 
> 
> 
> --
> 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 7 in thread

connect(treeJrn->selectionModel(), SIGNAL(currentRowChanged(const 
QModelIndex&,const QModelIndex&)), SLOT(onJrnItemChanged(const 
QModelIndex&)));

Sorry that it was missing.

--
 [ signature omitted ] 

Message 8 in thread

Then this is wrong:

>void TMainView::onJrnItemChanged(const QModelIndex& idx)
>{
>	QStandardItem* item = m_Model->item(idx.row());
>	...
>}

because the index given refers to the selectionmodel i.e. the view's
model, i.e. the proxy.
try this instead:

>	QStandardItem* item = m_Proxy->item(idx.row());

Cheers,
Peter

> -----Original Message-----
> From: J. Preiss [mailto:auba@xxxxxxx]
> Sent: Thursday, May 24, 2007 3:17 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: How to set the current inde xof a list?
> 
> connect(treeJrn->selectionModel(), SIGNAL(currentRowChanged(const
> QModelIndex&,const QModelIndex&)), SLOT(onJrnItemChanged(const
> QModelIndex&)));
> 
> Sorry that it was missing.

--
 [ signature omitted ] 

Message 9 in thread

> try this instead:
> >	QStandardItem* item = m_Proxy->item(idx.row());

This function does not exist. But you are partially right:

	QModelIndex index = m_Proxy->mapToSource(idx);
	QStandardItem* item = m_Model->item(index.row());

would be the right way, but crashes. 

	QStandardItem* item = m_Model->item(idx.row());

does not crash but is wrong. I have the choice...

--
 [ signature omitted ] 

Message 10 in thread

> > try this instead:
> > >	QStandardItem* item = m_Proxy->item(idx.row());
> 
> This function does not exist. But you are partially right:
> 
> 	QModelIndex index = m_Proxy->mapToSource(idx);
> 	QStandardItem* item = m_Model->item(index.row());
> 
> would be the right way, but crashes.
> 
> 	QStandardItem* item = m_Model->item(idx.row());
> 
> does not crash but is wrong. I have the choice...

This is a very funny choice you're doing there :)

If the right function crashes, why not find out the reason it crashes
and fix that, instead of using some other non-crashing function that
doesn't do what you want?

Ok back on track, what do you mean by "crashes"?
can you show a stack trace? maybe this is called before m_Proxy is set?

Cheers,
Peter

--
 [ signature omitted ] 

Message 11 in thread

> This is a very funny choice you're doing there :)

This was explained before. I used the working but wrong method to look for 
the "delete" method, which uses a current (mapped) index, too. And there it 
works.

> If the right function crashes, why not find out the reason it crashes
> and fix that, instead of using some other non-crashing function that
> doesn't do what you want?

Because I assume my code to be wrong, instead of assuming TTs code to be 
wrong... 

> Ok back on track, what do you mean by "crashes"?
> can you show a stack trace? maybe this is called before m_Proxy is set?

if i'd only know how to copy the stack trace of kdevelop... :-(

From crash down:

-----------------
QModelIndex QSortFilterProxyModelPrivate::proxy_to_source(const QModelIndex 
&proxy_index) const
{
    if (!proxy_index.isValid())
        return QModelIndex(); // for now; we may want to be able to set a root 
index later
    IndexMap::const_iterator it = index_to_iterator(proxy_index);
    Mapping *m = it.value();
-------------------
QModelIndex QSortFilterProxyModel::mapToSource(const QModelIndex &proxyIndex) 
const
{
    Q_D(const QSortFilterProxyModel);
    return d->proxy_to_source(proxyIndex);
-------------------
void TMainView::onJrnItemChanged(const QModelIndex& idx)
{
	bool show = idx.isValid() ? true : false;
	buttonAdd->setEnabled(false);
	buttonChange->setEnabled(show);
	buttonDel->setEnabled(show);
	if( !idx.isValid() )
		return;
	QModelIndex index = m_Proxy->mapToSource(idx); 


(complete code from sourceforge: 
 cvs -z3 -d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/cvsroot/qhelpers 
co -P qthelpers)

--
 [ signature omitted ] 

Message 12 in thread

> Because I assume my code to be wrong, instead of assuming TTs code to
be
> wrong...

I'm not saying TTs code is wrong ;) it may be the way you call it that
causes the crash. as you said in delete event it works fine.

> > Ok back on track, what do you mean by "crashes"?
> > can you show a stack trace? maybe this is called before m_Proxy is
set?
> 
> if i'd only know how to copy the stack trace of kdevelop... :-(
> 
> From crash down:

what interests me more: what is calling onJrnItemChanged? maybe this is
in your TMainView constructor? And maybe m_Proxy is not assigned yet?
i have no idea how your TMainView constructor works.

it would really help to have a minimal compiling example of the problem
(and no, i don't want to download your entire project)

strip it down to "minimal" for two reasons:
- you might find the error while stripping it down
- if you have it stripped down, its much easier for us to find the issue

Cheers,
Peter

--
 [ signature omitted ] 

Message 13 in thread

On 24.05.07 17:00:22, J. Preiss wrote:
> > Ok back on track, what do you mean by "crashes"?
> > can you show a stack trace? maybe this is called before m_Proxy is set?
> 
> if i'd only know how to copy the stack trace of kdevelop... :-(

a) run your app directly in gdb from a terminal
b) type "bt" in the GDB window, right click it and select copy

Seems like the framestack widget doesn't support copy.

> (complete code from sourceforge: 
>  cvs -z3 -d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/cvsroot/qhelpers 
> co -P qthelpers)

andreas@morpheus:~/temp>cvs -z3 -d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/cvsroot/qhelpers co -P qthelpers
cvs [checkout aborted]: connect to qhelpers.cvs.sourceforge.net(66.35.250.96):2401 failed: Connection refused

Andreas 

-- 
 [ signature omitted ] 

Message 14 in thread

I think in your case ModelTest class would be helpfull... check  
http://labs.trolltech.com/page/Projects/Itemview/Modeltest

On May 24, 2007, at 5:37 PM, Andreas Pakulat wrote:

> On 24.05.07 17:00:22, J. Preiss wrote:
>>> Ok back on track, what do you mean by "crashes"?
>>> can you show a stack trace? maybe this is called before m_Proxy  
>>> is set?
>>
>> if i'd only know how to copy the stack trace of kdevelop... :-(
>
> a) run your app directly in gdb from a terminal
> b) type "bt" in the GDB window, right click it and select copy
>
> Seems like the framestack widget doesn't support copy.
>
>> (complete code from sourceforge:
>>  cvs -z3 -d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/ 
>> cvsroot/qhelpers
>> co -P qthelpers)
>
> andreas@morpheus:~/temp>cvs -z3 - 
> d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/cvsroot/qhelpers  
> co -P qthelpers
> cvs [checkout aborted]: connect to qhelpers.cvs.sourceforge.net 
> (66.35.250.96):2401 failed: Connection refused
>
> Andreas
>
> -- 
> You're at the end of the road again.
>
> --
> 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 15 in thread

Many thanks to both of you!

> I think in your case ModelTest class would be helpfull... check
> http://labs.trolltech.com/page/Projects/Itemview/Modeltest

Great. Running the tests, I get problems in the line
    Q_ASSERT(flags == Qt::ItemIsDropEnabled || flags == 0);

When I comment the line out, I get a segfault. I'm looking for the flags right 
now..

> a) run your app directly in gdb from a terminal
> b) type "bt" in the GDB window, right click it and select copy

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1225378096 (LWP 7189)]
0xb73c9902 in QMetaObject::activate (sender=0x80f6c70, from_signal_index=5, 
to_signal_index=5, argv=0xbf120184) at kernel/qobject.cpp:2985
warning: Source file is more recent than executable.
2985    void QMetaObject::activate(QObject *sender, int from_signal_index, int 
to_signal_index, void **argv)
(gdb) bt
#0  0xb73c9902 in QMetaObject::activate (sender=0x80f6c70, 
from_signal_index=5, to_signal_index=5, argv=0xbf120184) at 
kernel/qobject.cpp:2985
#1  0xb73ca8c2 in QMetaObject::activate (sender=0x80f6c70, m=0xb7464f38, 
local_signal_index=1, argv=0xbf120184) at kernel/qobject.cpp:3125
#2  0xb73e91cf in QAbstractItemModel::headerDataChanged (this=0x80f6c70, 
_t1=Qt::Horizontal, _t2=0, _t3=0) 
at .moc/release-shared/moc_qabstractitemmodel.cpp:130
#3  0xb7cd3506 in QSortFilterProxyModelPrivate::_q_sourceHeaderDataChanged 
(this=0x810fd08, orientation=Qt::Horizontal, start=0, end=0) at 
itemviews/qsortfilterproxymodel.cpp:1009
#4  0xb7cd7acb in QSortFilterProxyModel::qt_metacall (this=0x80f6c70, 
_c=QMetaObject::InvokeMetaMethod, _id=6, _a=0xbf1206f4) 
at .moc/release-shared/moc_qsortfilterproxymodel.cpp:119
#5  0xb73c9d62 in QMetaObject::activate (sender=0x80cecb0, 
from_signal_index=5, to_signal_index=5, argv=<value optimized out>) at 
kernel/qobject.cpp:3066
#6  0xb73ca8c2 in QMetaObject::activate (sender=0x80cecb0, m=0xb7464f38, 
local_signal_index=1, argv=0xbf1206f4) at kernel/qobject.cpp:3125
#7  0xb73e91cf in QAbstractItemModel::headerDataChanged (this=0x80cecb0, 
_t1=Qt::Horizontal, _t2=0, _t3=0) 
at .moc/release-shared/moc_qabstractitemmodel.cpp:130
#8  0xb7cdcffa in QStandardItemModelPrivate::itemChanged (this=0x80f6b98, 
item=0x8246e50) at itemviews/qstandarditemmodel.cpp:441
#9  0xb7cddb4e in QStandardItem::setData (this=0x8246e50, value=@0xbf1209fc, 
role=2) at itemviews/qstandarditemmodel.cpp:772
#10 0xb7ce1eef in QStandardItemModel::setHeaderData (this=0x80cecb0, 
section=0, orientation=Qt::Horizontal, value=@0xbf1209fc, role=2) at 
itemviews/qstandarditemmodel.cpp:2782
#

and so on... 

> andreas@morpheus:~/temp>cvs -z3 -
> d:pserver:anonymous@xxxxxxxxxxxxxxxxxxxxxxxxxxxx:/cvsroot/qhelpers
> co -P qthelpers
> cvs [checkout aborted]: connect to qhelpers.cvs.sourceforge.net
> (66.35.250.96):2401 failed: Connection refused

It seems, that sourceforge is partially down - although I can get the sources 
when I checkin/out. Strange. 

--
 [ signature omitted ] 

Pages: Prev | 1 | 2 | Next