Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: qt4 QStringList with model/view

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default qt4 QStringList with model/view

    Qt Code:
    1. QStringList Destinataires::getDestList()
    2. {
    3. int rowCount = model->rowCount();
    4. int i;
    5.  
    6. QItemSelectionModel *sel = qDestin->selectionModel();
    7. for( i = 0; i < rowCount; i++ )
    8. if ( sel->isRowSelected( i, qDestin->rootIndex() ) )
    9. s = model->data( model->index( i, 0 ) ); <- compile error
    10. rows.push_back( s );
    11. return rows;
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. 96 C:\Qt\test\sms\destin.cpp no match for 'operator=' in 's = *(((Destinataires*)this)->Destinataires::model->QAbstractItemModel::_vptr$QObject + 68u)(((const QModelIndex&)((const QModelIndex*)(&*(((Destinataires*)this)->Destinataires::model->QAbstractItemModel::_vptr$QObject + 48u)(i, 0, ((const QModelIndex&)((const QModelIndex*)(&QModelIndex()))))))), 0)'
    To copy to clipboard, switch view to plain text mode 

    What should I do to retrieve the QString s from the model ?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Qt Code:
    1. QStringList Destinataires::getDestList()
    2. {
    3. QModelIndexList selected = selectionModel()->selectedIndexes();
    4. foreach (QModelIndex idx, selected)
    5. {
    6. // you only want strings in 1st column? the list of selection
    7. // indexes contain indexes for possible rest of colums also..
    8. if (idx.row() == 0)
    9. {
    10. rows << idx.data().toString();
    11. }
    12. }
    13. return rows;
    14. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    with
    Qt Code:
    1. QModelIndexList selected = selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

    I get compile error :
    89 C:\Qt\test\sms\destin.cpp `((Destinataires*)this)->Destinataires::selectionModel' cannot be used as a function

    Sorry I am completly a beginner in QT4 and model/views.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Right, I missed the qDestin ptr.. How about this:
    Qt Code:
    1. QModelIndexList selected = qDestin->selectionModel()->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jpn for this useful post:

    incapacitant (15th March 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    Yes what you say works perfectly. QT4 is hard to understand for me.

  7. #6
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    As I want all rows your code has become :
    Qt Code:
    1. QStringList Destinataires::getDestList()
    2. {
    3. QStringList rows;
    4. QModelIndexList selected = qDestin->selectionModel()->selectedIndexes();
    5. foreach (QModelIndex idx, selected)
    6. rows << idx.data().toString();
    7.  
    8. return rows;
    9. }
    To copy to clipboard, switch view to plain text mode 


    But there you declare "QModelIndexList". What does it mean ?
    Related Non-Memberstypedef QModelIndexList
    Synonym for QList<QModelIndex>.
    Completly lost.

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    As docs say, it's just a synonym for a QList containing QModelIndexes.

    All view's have a selection model which keeps track of selected items.
    There might be multiple selected "items", so the selection cannot just be a single "item".
    So the view's selection model needs to return a list of selected indexes.

    QModelIndexList QItemSelectionModel::selectedIndexes () const

    Returns a list of all selected model item indexes. The list contains no duplicates, and is not sorted.

  9. The following user says thank you to jpn for this useful post:

    incapacitant (15th March 2006)

  10. #8
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    There is a pre-defined type for all QList ?
    Here we have QList<QStrings>.
    Would it be still the same QModelIndexList ?
    with QList<Int>, or another type ?

  11. #9
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Quote Originally Posted by incapacitant
    There is a pre-defined type for all QList ?
    Here we have QList<QStrings>.
    Would it be still the same QModelIndexList ?
    with QList<Int>, or another type ?
    Actually, QStringList and QList<QString> are different. QStringList inherits QList<QString> and adds some functionality..

    Because QList is a template class, you can have a list of basically anything, as long as it is an assignable data type.

    There are a few type definitions: QVariantList, QObjectList, QModelIndexList..
    You can take them as a more convenient way of writing the type or something..

    There is an essential reason for these type definitions, and I think it has something to do with Qt's meta-object system, only registered meta types can be used in signal and slot connections..
    But that's a whole different world to discuss

  12. The following user says thank you to jpn for this useful post:

    incapacitant (15th March 2006)

  13. #10
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    Well now the next question is how do I retrieve the ith item number of a QStringList ?

    (i am lost with this)

  14. #11
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Take a look at the documentation and see what methods QList offers..

    You can use for example method at(int i) or operator [].
    Qt Code:
    1. QString row = rows.at(i);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString row = rows[i];
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to jpn for this useful post:

    incapacitant (15th March 2006)

  16. #12
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    In this code I try to copy selected items from dialog menu into dialog destination.

    Qt Code:
    1. for ( int i = 3; i < model->rowCount(); i++ )
    2. {
    3. s = model->rows.at(i);
    4. row = Destinataires_dlg.model->rowCount();
    5. Destinataires_dlg.model->insertRow( row );
    6. Destinataires_dlg.model->setData(model->index(row, 0), s );
    7. }
    To copy to clipboard, switch view to plain text mode 


    but i cannot find the right member to extract each row from model (in menu).

    Can you help me ?

  17. #13
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Sorry, but I don't quite get it what you are trying to do, but why don't you just use the same model for the destination view?
    Or is there any specific reason why couldn't you use the same model..?

  18. The following user says thank you to jpn for this useful post:

    incapacitant (15th March 2006)

  19. #14
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    First I am sorry because I am very poor at QT.

    I have a menu dialog with users listed that calls a destin dialog, both within different classes.
    Destin dialog allows the user to select items, as this thread explained to me and go back to my menu dialog (while closing the destin window) and brings back selected items that I list on the menu dialog.

    Then before calling again Destin I want to identify the entries of the menu that are on the destin model to highlight them as already selected.
    So I go thru the menu dialog for entries to highlight.

    Qt Code:
    1. Destinataires Destinataires_dlg( this );
    2. if ( model->rowCount() > 3 )
    3. {
    4. for ( int i = 3; i < model->rowCount(); i++ )
    5. {
    6. // find entry to highlight from the menu model
    7. s = model->rows.at(i);
    8. // then I should find the same entry in destin model to mark it as selected
    9. // but I don't know how to do that in QT4, worked very well in QT3
    10. // here I have to do a loop to search s in destinataires_dlg.model
    11. // and highlight it
    12. row = Destinataires_dlg.model->rowCount();
    13. Destinataires_dlg.model->insertRow( row );
    14. Destinataires_dlg.model->setData(model->index(row, 0), s );
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    nb in qt3 the code was :
    Qt Code:
    1. if ( qRecap->count() > 3 )
    2. {
    3. for ( int i = 3; i < qRecap->count(); i++ )
    4. {
    5. QListBoxItem *itemQRecap = qRecap->item( i );
    6. for ( int j = 0; j < Destinataires_dlg.qDestinCount(); j++ )
    7. {
    8. QListBoxItem *itemQDestin = Destinataires_dlg.qDestinItem( j );
    9. // si item de destinataires est dans le recapitulatif, on le selectionne
    10. if ( itemQRecap->text() == itemQDestin->text() )
    11. Destinataires_dlg.qDestinSelect( itemQDestin );
    12. }
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by incapacitant; 15th March 2006 at 15:53.

  20. #15
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Looking for something like this?
    Attached Files Attached Files

  21. The following user says thank you to jpn for this useful post:

    incapacitant (16th March 2006)

  22. #16
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    This is precisely what I was trying to do. Thank you very much.
    Trouble is I don't understand how you make it work :

    I don't understand how the selector form contains still the selection when you go back to the selector form. My constructor reinitializes the content and does not remember what was selected before.

    That is why I was trying to iterate through the rows of the main form : to reselect manually the selector entries. Why your constructor does not re-init the selection, but displays what was selected before is a mistery for me.
    Last edited by incapacitant; 16th March 2006 at 07:05.

  23. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Quote Originally Posted by incapacitant
    For instance I don't understand how the selector form contains the selection when you go back to the selector form. My constructor reinitializes the content and does not remember what was selected before.
    Because in the example the selector form is created only once. The select-button executes the same form again and again. So the selector form can remember it's state between "launches".

  24. #18
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    How do you ensure it is launched only once ? Because the selector call is in the mainform constructor ? I am lost.

  25. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: qt4 QStringList with model/view

    Yes, the selector dialog is created only once, in the mainwindow constructor.
    In the same constructor, also a connection (guess which ) is made which causes the select button click to launch the selector dialog as modal.

  26. The following user says thank you to jpn for this useful post:

    incapacitant (16th March 2006)

  27. #20
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qt4 QStringList with model/view

    Ok, then I should be able to adapt your solution to my code.
    Thanks a lot.

Similar Threads

  1. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  2. QStringList scope problem
    By ht1 in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 19:44
  3. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01
  4. Cannot queue arguments of type 'QStringList'
    By vfernandez in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2006, 20:48
  5. need help to classify some QStringList
    By patcito in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 21:24

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.