Results 1 to 10 of 10

Thread: selectRow()

  1. #1
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question selectRow()

    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:

    Qt Code:
    1. if(model->rowCount()){
    2. for(int row=0; row<model->msgList.size();++row){
    3. const QVcaCanMsg &msg = model->msgList.at(row);
    4. if((msg.id()==id) && (msg.flags()==flags)){
    5. msgLogView->selectRow(row);
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by gyre; 11th December 2007 at 19:42. Reason: updated contents

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    Use:
    Qt Code:
    1. msgLogView->setSelectionMode(QAbstractItemView::MultiSelection);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: selectRow()

    Quote Originally Posted by gyre View Post
    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:

    Qt Code:
    1. if(model->rowCount()){
    2. for(int row=0; row<model->msgList.size();++row){
    3. const QVcaCanMsg &msg = model->msgList.at(row);
    4. if((msg.id()==id) && (msg.flags()==flags)){
    5. msgLogView->selectRow(row);
    6. }
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    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.

  4. #4
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    Quote Originally Posted by marcel View Post
    Use:
    Qt Code:
    1. msgLogView->setSelectionMode(QAbstractItemView::MultiSelection);
    To copy to clipboard, switch view to plain text mode 
    didnt work

  5. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    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.

  6. #6
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    Quote Originally Posted by wysota View Post
    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...

    Quote Originally Posted by wysota View Post
    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

    Quote Originally Posted by wysota View Post
    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

  7. #7
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    Quote Originally Posted by marcel View Post
    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...

  8. #8
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    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:

    Qt Code:
    1. if(model->rowCount()){
    2. for(int row=0; row<model->rowCount();++row){
    3. QModelIndex index0 = model->index(row, 0);
    4. QModelIndex index1 = model->index(row, 1);
    5. if((model->data(index0).toInt()==id) && (model->data(index1).toInt()==flags)){
    6. msgLogView->selectRow(row);
    7. }
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Im comparing values in column 0 and 1...they are Integers
    Last edited by gyre; 11th December 2007 at 20:43. Reason: updated contents

  9. #9
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: selectRow()

    Quote Originally Posted by marcel View Post
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: selectRow()

    Quote Originally Posted by gyre View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.