Qt-interest Archive, May 2007
Wrong item in QItemView::item(row)
Pages: Prev | 1 | 2 | Next
Message 16 in thread
On 22.05.07 16:02:50, J. Preiss wrote:
> On Tuesday 22 May 2007 15:48:28 Andreas Pakulat wrote:
> > On 22.05.07 14:38:38, J. Preiss wrote:
> > > > how about:
> > > >
> > > > model->insertRow(model->rowCount());
> > >
> > > I tried this before I wrote to the qtinterest.
> > > When I use it, I see the last line at the top of the view, followed by
> > > <count> empty lines (I see them because the root is decorated).
> >
> > Its rather inserRow(rowCount()-1), if that has the same effect that
> > sounds like a Bug in Qt. What Qt version do you use?
>
> No, it is rowCount. (obviously, because rows=0 when empty)
Ah right.
Andreas
--
[ signature omitted ]
Message 17 in thread
On 22.05.07 13:54:55, J. Preiss wrote:
> > Your assumption:
> > Normally I would assume that the first row contains the first item
>
> No, my assumption was: currentChange(QModelIndex) will be called with
> the "right" index. I did not know that I have to translate the index first.
> My fault.
You don't necessarily have to. It depends on wether you have the view
connected to the real model (the one with the data) or a proxy model
that does some sorting or filtering.
Also there's no such signal, currentChanged has 2 parameters.
> I am wondering if it is strange that I have to translate the index of the
> treeJrn->currentIndex, too. Or is it logic?
> When I get the current index of a view, it is the index into the proxy
> model... mmmh... yeah, ok.
Of course only if you've set a proxy model. If you didn't do that then
the view uses the real model with the data you created.
> to your appendRow idea: yes, I'd had preferred a call to appendRow, but
> without parameters:
>
> appendRow();
> setData(...
>
> -> and this method is missing.
Why? Why can't you create a QStandardItem and hand it to appendRow()?
You could as well do something like insertRow(rowCount()-1) and
setData(index(rowCount()-1,0)...) to append new indices.
Andreas
--
[ signature omitted ]
Message 18 in thread
On 22.05.07 10:48:41, J. Preiss wrote:
> > then after finishing the loop the model has:
> > row text userdata
> > 0 381 380
> > 1 380 379
> > ...
> > 380 1 0
> >
> > Which is (as far as I see) exactly what you get. If you want
> > userdata==text-1==row then you have to count backwards or always insert
> > at the end of the model.
>
> This is probably all true, but I get a signal currentChanged with an index,
> and I try to get correspondant item in the view with that index and get a
> wrong item.
Actually you get 2 indexes in the currentChanged signal. But with just
these explanation at least I can't really help you. Try to put together
a minimal compilable example so that we can see whats happening.
> At the end this would mean I always have to look for the right sort ordering,
> how I add my items to my model, to get the right item in my views.
The index you get from the selection model is the index into the model
of your view. So if you ask the model for that data of that index you
should get the same data you can see in the view. Of course things get
more complicated if the model that the view uses is a sorting of
filtering proxy model, then you'd first have to convert the index into
the source model index and with that you can go to the source model and
ask it for its data. But without the real code where you setup the view
and the model nobody (except you) knows wether you have a proxy model or
not.
Andreas
--
[ signature omitted ]
Message 19 in thread
> ask it for its data. But without the real code where you setup the view
> and the model nobody (except you) knows wether you have a proxy model or
> not.
You are right. I am sure I mentioned that somewhere, but: yes, I use a sorting
proxy.
Now I added
QModelIndex index =
m_Proxy->mapToSource(idx); //static_cast<QSortFilterProxyModel*>(treeJrn->model())->mapToSource(idx);
QStandardItem* item = m_Model->item(index.row());
and the application crashes 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();
I
--
[ signature omitted ]
Message 20 in thread
On 22.05.07 13:49:19, J. Preiss wrote:
> > ask it for its data. But without the real code where you setup the view
> > and the model nobody (except you) knows wether you have a proxy model or
> > not.
>
> You are right. I am sure I mentioned that somewhere, but: yes, I use a sorting
> proxy.
> Now I added
>
> QModelIndex index =
> m_Proxy->mapToSource(idx); //static_cast<QSortFilterProxyModel*>(treeJrn->model())->mapToSource(idx);
> QStandardItem* item = m_Model->item(index.row());
>
> and the application crashes 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();
>
> I
Wnad which line exactly? Do you always check that the indexes you use
are valid?
I really suggest you come up with a minimal compilable example instead
of snippets here and there. I don't see anything wrong in your snippet
and certainly have no idea why it crashes.
Andreas
--
[ signature omitted ]
Message 21 in thread
> QStandardItem* item = m_Model->item(idx.row());
I replaced it by
QStandardItem* item = m_Model->itemFromIndex(idx);
without any change.
I really don't understand, what insertRow function the "Programming Qt4" book
calls...
void MyStandardItemModel::addNote(const TRelNote& note)
{
insertRow(0);
setData(index(0, 0), note.getId(), Qt::UserRole);
...
There are two insertRow methods, but none of them has standard parameters set.
What I probably really want is to call an append method, but then I have to
construct all QStandardItems for myself... ???
--
[ signature omitted ]
Message 22 in thread
On 20.05.07 22:45:10, J. Preiss wrote:
> > QStandardItem* item = m_Model->item(idx.row());
>
> I replaced it by
> QStandardItem* item = m_Model->itemFromIndex(idx);
> without any change.
>
> I really don't understand, what insertRow function the "Programming Qt4" book
> calls...
The one of the base class, whichever that is. I don't have the book but
the code snippet you present below looks like they expect you to
subclass QAbstractItemModel and not QStandardItemModel.
> void MyStandardItemModel::addNote(const TRelNote& note)
> {
> insertRow(0);
> setData(index(0, 0), note.getId(), Qt::UserRole);
> ...
> There are two insertRow methods, but none of them has standard parameters set.
There are 3, the third one is in QAbstractItemModel, which is the base
class of QStandardItemModel and that one calls insertRows() with proper
parameters.
The code above should create a new row at the top of your model and then
set its userrole data to note.getId().
> What I probably really want is to call an append method, but then I have to
> construct all QStandardItems for myself... ???
And whats so bad about that?
Andreas
--
[ signature omitted ]
Message 23 in thread
On Monday 21 May 2007 00:16:42 Andreas Pakulat wrote:
> On 20.05.07 22:45:10, J. Preiss wrote:
> > > QStandardItem* item = m_Model->item(idx.row());
> >
> > I replaced it by
> > QStandardItem* item = m_Model->itemFromIndex(idx);
> > without any change.
> >
> > I really don't understand, what insertRow function the "Programming Qt4"
> > book calls...
>
> The one of the base class, whichever that is. I don't have the book but
> the code snippet you present below looks like they expect you to
> subclass QAbstractItemModel and not QStandardItemModel.
I am sorry, I was completely wrong. I searched the complete book's chapter to
find out, that the snippet is from *your* own example. Have a look in
qt4/examples/itemviews/basicsortfiltermodel/main.cpp. You'll find there a
QStandardItemModel together with the above code :-)
And in all theses examples I did not find anything to do with "current item
changed" :-(
> > There are two insertRow methods, but none of them has standard parameters
> > set.
>
> There are 3, the third one is in QAbstractItemModel, which is the base
> class of QStandardItemModel and that one calls insertRows() with proper
> parameters.
--- List of All Members for QStandardItemModel ---
...
insertRow ( int, const QList<QStandardItem *> & )
insertRow ( int, QStandardItem * )
insertRows ( int, int, const QModelIndex & ) : bool
...
obviously, that "all" is wrong, because
--- List of All Members for QAbstractItemModel ---
...
insertRow ( int, const QModelIndex & ) : bool
insertRows ( int, int, const QModelIndex & ) : bool
...
>
> The code above should create a new row at the top of your model and then
> set its userrole data to note.getId().
>
> > What I probably really want is to call an append method, but then I have
> > to construct all QStandardItems for myself... ???
>
> And whats so bad about that?
Is that what Qt4 wants me to? In qt4, there are a few ways to Rome, but the
hint, which way you best use, is hidden... do I better add items in the
dialog class, or better in the model class... I think you could write
complete books about that model/view stuff, and I wish you would...
--
[ signature omitted ]
Message 24 in thread
On 21.05.07 11:13:38, J. Preiss wrote:
> On Monday 21 May 2007 00:16:42 Andreas Pakulat wrote:
> > On 20.05.07 22:45:10, J. Preiss wrote:
> > > > QStandardItem* item = m_Model->item(idx.row());
> > >
> > > I replaced it by
> > > QStandardItem* item = m_Model->itemFromIndex(idx);
> > > without any change.
> > >
> > > I really don't understand, what insertRow function the "Programming Qt4"
> > > book calls...
> >
> > The one of the base class, whichever that is. I don't have the book but
> > the code snippet you present below looks like they expect you to
> > subclass QAbstractItemModel and not QStandardItemModel.
>
> I am sorry, I was completely wrong. I searched the complete book's chapter to
> find out, that the snippet is from *your* own example.
This is not _my_ example, that is an example from TT shipped within the
Qt sources.
> > > There are two insertRow methods, but none of them has standard parameters
> > > set.
> >
> > There are 3, the third one is in QAbstractItemModel, which is the base
> > class of QStandardItemModel and that one calls insertRows() with proper
> > parameters.
>
> --- List of All Members for QStandardItemModel ---
> ...
> insertRow ( int, const QList<QStandardItem *> & )
> insertRow ( int, QStandardItem * )
> insertRows ( int, int, const QModelIndex & ) : bool
> ...
>
> obviously, that "all" is wrong, because
No, its not, that are all members of the class QStandardItemModel, that
doesn't include inherited members, there's a separate list for that to
not clutter the API documentation.
> > The code above should create a new row at the top of your model and then
> > set its userrole data to note.getId().
> >
> > > What I probably really want is to call an append method, but then I have
> > > to construct all QStandardItems for myself... ???
> >
> > And whats so bad about that?
>
> Is that what Qt4 wants me to? In qt4, there are a few ways to Rome, but the
> hint, which way you best use, is hidden...
Qt4 offers you various ways so that you can use the one which fits your
needs best. The standarditemmodel is mainly meant to be used when you
need a model but don't have the need for a custom datastructure behind
the model, i.e. all your data fits nicely into the standard items. If
that doesn't work you'd want to write your own subclass of
QAbstractItemModel (or the list/table subclasses).
> do I better add items in the dialog class, or better in the model
> class...
If you're using QStandardItemModel there shouldn't be a need to write
your own model class. All you need to do is create a QStandardItem set
its text and possibly the user data and add it to the standard model.
> I think you could write complete books about that model/view stuff,
> and I wish you would...
I guess you mean TT here, note that I'm not a TT member.
Andreas
--
[ signature omitted ]
Message 25 in thread
> This is not _my_ example, that is an example from TT shipped within the
> Qt sources.
I am sorry. I don't know how I could think you are from TT :-)
> > obviously, that "all" is wrong, because
>
> No, its not, that are all members of the class QStandardItemModel, that
Yes it is. I only have one link. "List of all members, including inherited
members "
> > Is that what Qt4 wants me to? In qt4, there are a few ways to Rome, but
> > the hint, which way you best use, is hidden...
>
> Qt4 offers you various ways so that you can use the one which fits your
> needs best. The standarditemmodel is mainly meant to be used when you
> need a model but don't have the need for a custom datastructure behind
> the model, i.e. all your data fits nicely into the standard items. If
> that doesn't work you'd want to write your own subclass of
> QAbstractItemModel (or the list/table subclasses).
But then it would be great if TT would use it the same way in its examples...
--
[ signature omitted ]
Message 26 in thread
On 21.05.07 13:20:46, J. Preiss wrote:
> > > obviously, that "all" is wrong, because
> >
> > No, its not, that are all members of the class QStandardItemModel, that
>
> Yes it is. I only have one link. "List of all members, including inherited
> members "
Indeed, I didn't look at the page with the inherited members. This is
clearly a documentation bug, please file a bugreport at qt-bugs at
trolltech dot com.
> > > Is that what Qt4 wants me to? In qt4, there are a few ways to Rome, but
> > > the hint, which way you best use, is hidden...
> >
> > Qt4 offers you various ways so that you can use the one which fits your
> > needs best. The standarditemmodel is mainly meant to be used when you
> > need a model but don't have the need for a custom datastructure behind
> > the model, i.e. all your data fits nicely into the standard items. If
> > that doesn't work you'd want to write your own subclass of
> > QAbstractItemModel (or the list/table subclasses).
>
> But then it would be great if TT would use it the same way in its examples...
So you mean they should create the standarditems? Why? They don't use
the items afterwards, so there's no reason to create them and have a
variable for that. The abstract item model API works fine for that
example.
Andreas
--
[ signature omitted ]