Results 1 to 4 of 4

Thread: How to programmatically change the Index of comboBox using QStandardItemModel

  1. #1
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to programmatically change the Index of comboBox using QStandardItemModel

    How to use the QStandardItemModel :: setData function to set the current value for a combobox that is found in a certain cell for example at index (0,0) and call the ComboBoxItemDelegate :: setModelData function.
    I know that ComboBoxItemDelegate :: setModelData function is called when an item is selected from the combo box but my problem is that ComboBoxItemDelegate :: setModelData function is not called by calling the combobox::setData. So i want to call combobox::setData or (any other function that) programatically that will call ComboBoxItemDelegate :: setModelData
    Last edited by Ahmed Abdellatif; 31st October 2018 at 20:06.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,120
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to programmatically change the Index of comboBox using QStandardItemModel

    Delegates are used only for editing models from within a view - in other words, it is one way. When editing is finished, the editor (combobox) is destroyed and the view is updated with the text returned by the model using Qt::DisplayRole.

    I think what you are asking is "When I double-click on the cell to create the combobox and start editing, how do I set the combobox to the right index, based on the content in the model for that QModelIndex?"

    The solution is probably to derive your own QItemDelegate class and override the setEditorData() method. You would call the base class to actually load the strings into the combobox delegate, and then add your own code to set the combobox index to the right value. You can store and retrieve this value in your model using Qt::UserRole.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    Ahmed Abdellatif (2nd November 2018)

  4. #3
    Join Date
    Sep 2017
    Posts
    23
    Thanks
    8
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to programmatically change the Index of comboBox using QStandardItemModel

    first of all thank you for your interest and answering my question. I have got the point. But the part that is not clear for me is the last part that is "You can store and retrieve this value in your model using Qt::UserRole". I do not know what is the benefit from item role?

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,120
    Thanks
    235
    Thanked 656 Times in 646 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to programmatically change the Index of comboBox using QStandardItemModel

    I do not know what is the benefit from item role?
    Somehow you need to recover the combobox index (an integer) that is associated with the string that is the DisplayRole or EditRole data for the QModelIndex you are about to edit.

    One way to do it would be to search the set of strings that are loaded into the combobox (QCombobox::findText()). Another way is to store it in your model as a special integer field that you set when the delegate calls setData() on your model. setData() is going to give you the currently selected string, which you can then convert to the integer. The use of Qt::UserRole is a way for you to store and retrieve this integer easily.

    Qt Code:
    1. // Method 1: look up the string in the combobox
    2. void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
    3. {
    4. QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
    5.  
    6. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
    7. if ( pCombo )
    8. {
    9. QString displayStr = index.data( Qt::DisplayRole ).toString();
    10. int selected = pCombo->findText( displayStr );
    11. if ( selected > -1 )
    12. pCombo->setCurrentIndex( selected );
    13. }
    14. }
    15.  
    16. // Method 2: use Qt::UserRole to store the index in the model
    17. void MyComboDelegate::setEditorData( QWidget * pEditor, const QModelIndex & index ) const
    18. {
    19. QItemDelegate::setEditorData( pEditor, index ); // use base class to fill the strings
    20.  
    21. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
    22. if ( pCombo )
    23. {
    24. int selected = index.data( Qt::UserRole ).toInt();
    25. if ( selected > -1 )
    26. pCombo->setCurrentIndex( selected );
    27. }
    28. }
    29.  
    30. void MyComboDelegate::setModelData( QWidget * pEditor, QAbstractItemModel * pModel, const QModelIndex & index ) const
    31. {
    32. QItemDelegate::setModelData( pEditor, pModel, index ); // base class sets Qt::DisplayRole string
    33.  
    34. QComboBox * pCombo = qobject_cast< QComboBox * >( pEditor );
    35. if ( pCombo )
    36. {
    37. int selected = pCombo->currentIndex();
    38. pModel->setData( index, QVariant( selected ), Qt::UserRole );
    39. }
    40. }
    41.  
    42.  
    43. void MyModel::setData( const QModelIndex & index, const QVariant & value, int role )
    44. {
    45. if ( index.column() == 0 && role == Qt::UserRole )
    46. {
    47. int selected = value.toInt();
    48. // store "selected" as a field in your model for that row.
    49. }
    50. else if ( role == Qt::EditRole )
    51. {
    52. // do whatever you would do otherwise to update your model
    53. }
    54. }
    55.  
    56. QVariant MyModel::data( const QModelIndex & index, int role ) const
    57. {
    58. if ( index.column() == 0 && role == Qt::UserRole )
    59. {
    60. int selected; // retrieve "selected" from the field in your model for that row.
    61. return QVariant( selected );
    62. }
    63. else if ( role == Qt::DisplayRole ) (or EditRole or whatever)
    64. {
    65. // do whatever you would do otherwise to get data from your model
    66. }
    67. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 0
    Last Post: 31st October 2018, 00:04
  2. How to change run configurations programmatically?
    By rakeshthp in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2015, 15:25
  3. Replies: 5
    Last Post: 3rd February 2015, 11:35
  4. How to get index of a combobox delegate selection
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 21st July 2009, 16:37
  5. ComboBox Completer Index Problem
    By majatu in forum Qt Programming
    Replies: 3
    Last Post: 7th June 2009, 14:30

Tags for this Thread

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.