Qt-interest Archive, May 2007
Wrong item in QItemView::item(row)
Pages: Prev | 1 | 2 | Next
Message 1 in thread
I wrote it in another thread, but maybe the subject was wrong to get an
answer..
void TMainView::on_buttonDel_clicked()
{
QModelIndex idx = treeJrn->currentIndex();
if( !idx.isValid() )
return;
QStandardItem* item = m_Model->item(idx.row());
qDebug() << item->text();
int ret = QMessageBox::question(this, tr("Remove item"), tr("Are you sure to
remove item %1?").arg(item->text()), tr("Yes"), tr("No"));
if( ret )
return;
}
treeJrn is a QTreeView, m_Model is a QStandardItemModel. When I have a look at
idx.row(), this is the text of the first column ("1") of the item, and would
be the correct value. The resulting item although gives me as text "328",
which is the treeJrn->count()-idx.row();
So how can I get the correct value for the current item?
--
[ signature omitted ]
Message 2 in thread
On 20.05.07 21:01:45, J. Preiss wrote:
> I wrote it in another thread, but maybe the subject was wrong to get an
> answer..
>
> void TMainView::on_buttonDel_clicked()
> {
> QModelIndex idx = treeJrn->currentIndex();
> if( !idx.isValid() )
> return;
> QStandardItem* item = m_Model->item(idx.row());
> qDebug() << item->text();
> int ret = QMessageBox::question(this, tr("Remove item"), tr("Are you sure to
> remove item %1?").arg(item->text()), tr("Yes"), tr("No"));
> if( ret )
> return;
> }
>
> treeJrn is a QTreeView, m_Model is a QStandardItemModel. When I have a look at
> idx.row(), this is the text of the first column ("1") of the item, and would
> be the correct value. The resulting item although gives me as text "328",
> which is the treeJrn->count()-idx.row();
> So how can I get the correct value for the current item?
You need to provide more information, especially provide the values of
the questionable columns, that is the row-number+the text you get for
the item and what you'd expect. It might help to show the code that sets
up the model.
Andreas
--
[ signature omitted ]
Message 3 in thread
> You need to provide more information, especially provide the values of
> the questionable columns, that is the row-number+the text you get for
> the item and what you'd expect. It might help to show the code that sets
> up the model.
Together with the basic sort example, you should have the code how to setup
the model. I better explain it with the "changed" signal of the selection
model of the tree:
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;
}
In the model are currently 381 items. When the first item of the dialog is
selected, item's text is "381", the row is "0" and the id is "380" (text is
id+1 to avoid 0).
When the last item is selected, .I get the text "1", index "380" and the id
is "0".
If you want to debug the code, I can check it in, you can reach it from
sourceforge.net, project qhelpers/relnotes :-)
--
[ signature omitted ]
Message 4 in thread
On 21.05.07 11:26:55, J. Preiss wrote:
> > You need to provide more information, especially provide the values of
> > the questionable columns, that is the row-number+the text you get for
> > the item and what you'd expect. It might help to show the code that sets
> > up the model.
>
> Together with the basic sort example, you should have the code how to setup
> the model. I better explain it with the "changed" signal of the selection
> model of the tree:
>
> 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;
> }
>
> In the model are currently 381 items. When the first item of the dialog is
> selected, item's text is "381", the row is "0" and the id is "380" (text is
> id+1 to avoid 0).
> When the last item is selected, .I get the text "1", index "380" and the id
> is "0".
And what exactly is the problem now? According to your information
everything works as it should. for a given row you can retrieve the item
and get its text and custom data. According to your specification text
is id+1, which is true for the first and the last row as you describe
it.
What exactly is the problem now?
Andreas
--
[ signature omitted ]
Message 5 in thread
> And what exactly is the problem now? According to your information
> everything works as it should. for a given row you can retrieve the item
> and get its text and custom data. According to your specification text
> is id+1, which is true for the first and the last row as you describe
> it.
>
> What exactly is the problem now?
The problem is, that when I click the first item, the information of the last
item is shown. Normally I would assume that the first row contains the first
item, so that
QStandardItem* item1 = m_Model->item(0);
QStandardItem* item2 = treeJrn->firstChild();
-> item1==item2
(ok, firstChild is not actually a function, just for the idea behind).
Or do I have to get the information from a may existing sort-proxy-model?
--
[ signature omitted ]
Message 6 in thread
On 21.05.07 13:25:11, J. Preiss wrote:
> > And what exactly is the problem now? According to your information
> > everything works as it should. for a given row you can retrieve the item
> > and get its text and custom data. According to your specification text
> > is id+1, which is true for the first and the last row as you describe
> > it.
> >
> > What exactly is the problem now?
>
> The problem is, that when I click the first item, the information of the last
> item is shown. Normally I would assume that the first row contains the first
> item, so that
> QStandardItem* item1 = m_Model->item(0);
> QStandardItem* item2 = treeJrn->firstChild();
> -> item1==item2
> (ok, firstChild is not actually a function, just for the idea behind).
>
> Or do I have to get the information from a may existing sort-proxy-model?
But according to your mails you create the rows via doing
insertRow(0);
this will create a new row in front of the existing first row. So when
you do something like
for(i = 0; i < 381 ; i++)
{
insertRow(0);
setData(index(0,0),QString::number(i+1), Qt::DisplayRole);
setData(index(0,0),i,Qt::UserRole);
}
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.
Andreas
--
[ signature omitted ]
Message 7 in thread
> 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.
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.
--
[ signature omitted ]
Message 8 in thread
... just use appendRow() instead of insertRow() ...
No sorting needed. Got it now? Thanks.
Peter
> -----Original Message-----
> From: J. Preiss [mailto:auba@xxxxxxx]
> Sent: Tuesday, May 22, 2007 10:49 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Wrong item in QItemView::item(row)
>
> > 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.
> 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.
--
[ signature omitted ]
Message 9 in thread
On Tuesday 22 May 2007 11:53:20 Peter Prade wrote:
> ... just use appendRow() instead of insertRow() ...
>
> No sorting needed. Got it now? Thanks.
>
> Peter
Means creating all QStardItem entries by hand for each column, collecting them
to a list, and append them then.
Maybe I am too stupid to understand the Model/View stuff. I am sorry about
that. But I am looking for a good book to learn it. The existing examples do
not help :-(
Or I'll stay at QListWidget...
--
[ signature omitted ]
Message 10 in thread
I'm sorry, the problem is probably on our side, we just don't get what
the problem is that you're having.
However this entire thread has been filled with wrong assumptions on
your side and i just wanted to make sure one of them gets eliminated:
Your assumption:
Normally I would assume that the first row contains the first item
This is wrong, because you enter your items with insertRow(0,...)
(making the last item inserted the one that appears in the first row)
Solution: if you would use appendRow(...) instead, then your assumption
would be correct (the first item that you add into the model will now be
the one in the first row).
This would probably make it easier for you to understand the model and
for us to help you find the problem you're having with currentIndex.
Cheers,
Peter
> -----Original Message-----
> From: J. Preiss [mailto:auba@xxxxxxx]
> Sent: Tuesday, May 22, 2007 12:27 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: Wrong item in QItemView::item(row)
>
> On Tuesday 22 May 2007 11:53:20 Peter Prade wrote:
> > ... just use appendRow() instead of insertRow() ...
> >
> > No sorting needed. Got it now? Thanks.
> >
> > Peter
>
> Means creating all QStardItem entries by hand for each column,
collecting
> them
> to a list, and append them then.
> Maybe I am too stupid to understand the Model/View stuff. I am sorry
about
> that. But I am looking for a good book to learn it. The existing
examples
> do
> not help :-(
>
> Or I'll stay at QListWidget...
--
[ signature omitted ]
Message 11 in thread
> 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.
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.
to your appendRow idea: yes, I'd had preferred a call to appendRow, but
without parameters:
appendRow();
setData(...
-> and this method is missing.
--
[ signature omitted ]
Message 12 in thread
> > 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.
>
> 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.
>
> to your appendRow idea: yes, I'd had preferred a call to appendRow,
but
> without parameters:
>
> appendRow();
> setData(...
>
> -> and this method is missing.
how about:
model->insertRow(model->rowCount());
Cheers,
Peter
--
[ signature omitted ]
Message 13 in thread
> 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).
--
[ signature omitted ]
Message 14 in thread
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?
Andreas
--
[ signature omitted ]
Message 15 in thread
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)
--
[ signature omitted ]
Pages: Prev | 1 | 2 | Next