PDA

View Full Version : selectRow()



gyre
11th December 2007, 19:41
I need an advise...
I need a widget that would go throug the whole model and do some comparing with items in it...then when comparing passes I need to select a row on which the item is...
So I wrote this code:


if(model->rowCount()){
for(int row=0; row<model->msgList.size();++row){
const QVcaCanMsg &msg = model->msgList.at(row);
if((msg.id()==id) && (msg.flags()==flags)){
msgLogView->selectRow(row);
}
}
}

But the problem is that it selects one after another so only the last item that passes the comparing stays selected in a view...
Is there any way how to make it so ALL items that pass the comparing will get selected ?
Thanks

marcel
11th December 2007, 19:55
Use:


msgLogView->setSelectionMode(QAbstractItemView::MultiSelection );

wysota
11th December 2007, 19:56
I need a widget that would go throug the whole model and do some comparing with items in it...
A widget or a class? What is "it"? The model or the "widget"?


then when comparing passes I need to select a row on which the item is...
So I wrote this code:


if(model->rowCount()){
for(int row=0; row<model->msgList.size();++row){
const QVcaCanMsg &msg = model->msgList.at(row);
if((msg.id()==id) && (msg.flags()==flags)){
msgLogView->selectRow(row);
}
}
}
Very strange.... what is msgList? How is it related to what model->data() returns?[/quote]


But the problem is that it selects one after another so only the last item that passes the comparing stays selected in a view...
Is there any way how to make it so ALL items that pass the comparing will get selected ?
Thanks

You mean you want to select all items that have some data piece set to some value?

I'd surely use QAbstractItemModel::match() for that... I don't know how your model is organized, but it seems you should be using data() instead of that msgList object which probably shouldn't be accessible outside the model.

gyre
11th December 2007, 20:12
Use:


msgLogView->setSelectionMode(QAbstractItemView::MultiSelection );


didnt work :(

marcel
11th December 2007, 20:16
I think it is QAbstractItemVie::ExtendedSelection, if the selected items are not consecutive.
But you should also listen to what wysota said, the model data shouldn't be accessible outside the model like that. You could use model indexes instead.

gyre
11th December 2007, 20:20
A widget or a class? What is "it"? The model or the "widget"?

youve never seen a widget that has model-view implemented ??
Yes it is implemented in a class...



Very strange.... what is msgList? How is it related to what model->data() returns?
[/QUOTE]

model is a QAbstractTableModel...msgList is a QList of messages(simply QLIst of data) that are displayed by QTableView



You mean you want to select all items that have some data piece set to some value?

No I mean I will read some values from QDialog and iterate through all model data(in this case the model data is QList<notimportantwhat> msgList) and check for some values...
When the data on particular row(row is also the index of QList since the new items are appended to model data) match the condition I want to select that row...
It means that more than one row can match the given condition BUT I dont know how to slect MORE THAN ONE ROW...I can only select only one row :(

gyre
11th December 2007, 20:24
I think it is QAbstractItemVie::ExtendedSelection, if the selected items are not consecutive.
But you should also listen to what wysota said, the model data shouldn't be accessible outside the model like that. You could use model indexes instead.

yes of course...Im not gonna put this to app...just wanted to try something if that works...

gyre
11th December 2007, 20:40
I could use match method of the model...but it is not the problem to get the indexes...the problem is TO SET SELECTED items on particular indexes....
when the data stored on those indexes match some/value criteria....

my code now looks this way:


if(model->rowCount()){
for(int row=0; row<model->rowCount();++row){
QModelIndex index0 = model->index(row, 0);
QModelIndex index1 = model->index(row, 1);
if((model->data(index0).toInt()==id) && (model->data(index1).toInt()==flags)){
msgLogView->selectRow(row);
}
}
}
Im comparing values in column 0 and 1...they are Integers

gyre
11th December 2007, 20:41
I think it is QAbstractItemVie::ExtendedSelection, if the selected items are not consecutive.
But you should also listen to what wysota said, the model data shouldn't be accessible outside the model like that. You could use model indexes instead.

yes I know...Im just trying one thing in a test application...then it will be implemented in actual program when this will work

wysota
11th December 2007, 20:49
youve never seen a widget that has model-view implemented ??
I have but what you said suggests you want a widget that will iterate over the model and change some view. It's a bit confusing for me - a widget is usually meant to display something, not to calculate something - that's why I asked.


model is a QAbstractTableModel...msgList is a QList of messages(simply QLIst of data) that are displayed by QTableView
So what's the point of having the model if you ignore it and use your own access to the data?


No I mean I will read some values from QDialog and iterate through all model data(in this case the model data is QList<notimportantwhat> msgList) and check for some values...
Define "check for values"...


When the data on particular row(row is also the index of QList since the new items are appended to model data) match the condition I want to select that row...
It means that more than one row can match the given condition BUT I dont know how to slect MORE THAN ONE ROW...I can only select only one row :(

Have you seen QItemSelection or QItemSelectionModel classes?

I suggest you first redesign your model before doing anything else. You already got yourself into trouble and if you continue with this invalid approach, you'll get yourself into even more trouble. It's high time to stop and rethink the approach.

I don't know what your QVcaCanMsg holds, but it is probably a structure that contains fields of types that can be put into QVariant. In that case this is a perfect candidate for using QStandardItemModel. It's probably a good candidate regardless of that condition anyway as you can subclass QStandardItem and extend it with some things you need. Each field in the structure can be returnable (and settable) from the model using custom model roles. If you need some convenience methods to access the data, go ahead, but use QAbstractItemModel::data() internally. You'll have much less trouble and will get many nice things for free (no need to even implement your own model!). If you have trouble with understanding custom roles, go to my blog and read the articles related to the GL model. The series is not complete but the usage of custom roles is shown there.