Results 1 to 11 of 11

Thread: setEditorData() for QComboBox Delegate

  1. #1
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default setEditorData() for QComboBox Delegate

    Hi!

    I have been studying the Spin Box Delegate Example and I think I understood how it works.

    Now I am trying to create the delegate for a qcombobox.
    I have transformed all the SpinBox functions but I cannot do the same for the setEditorData().Any ideas?

    Qt Code:
    1. #include <QtGui>
    2. #include "delegate.h"
    3. #include <delegate.h>
    4. #include <comboboxdelegate.h>
    5.  
    6. ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
    7. : QItemDelegate(parent)
    8. {
    9. }
    10.  
    11. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
    12. const QModelIndex &/* index */) const
    13. {
    14. QComboBox *editor = new QComboBox(parent);
    15. editor->addItem("a");
    16. editor->addItem("b56");
    17. editor->addItem("cc");
    18. return editor;
    19. }
    20.  
    21. void ComboBoxDelegate::setEditorData(QWidget *editor,
    22. const QModelIndex &index) const
    23. {
    24. // QString str = index.model()->data(index, Qt::EditRole);
    25.  
    26. // QComboBox *comboBox = static_cast<QComboBox*>(editor);
    27. // comboBox->setValue(value);
    28. // comboBox->setCurrentIndex(value);
    29.  
    30.  
    31. }
    32.  
    33. void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    34. const QModelIndex &index) const
    35. {
    36. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    37. //comboBox->interpretText();//is this important for the QComboBox delegate??
    38. QString str = comboBox->currentText();
    39.  
    40. model->setData(index, str, Qt::EditRole);
    41. }
    42.  
    43. void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
    44. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    45. {
    46. editor->setGeometry(option.rect);
    47. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: setEditorData() for QComboBox Delegate

    Qt Code:
    1. comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    fatecasino (3rd March 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setEditorData() for QComboBox Delegate

    thanks!
    can i ask a question?
    the combobox appears only if I double-click the cell which has a combobox delegate.
    Is is possible to have the combobox always in the cell (even if that cell is not clicked at all). I would like to use the mouse wheel to change its value.

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

    Default Re: setEditorData() for QComboBox Delegate

    It's technically possible (it's called a "persistent editor") but you probably don't want that as having too many of those significantly slows down the application. It's better to reimplement editorEvent() in the delegate and force edit mode when a wheel event for an item is detected.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setEditorData() for QComboBox Delegate

    Thanks wysota! It worked:

    Qt Code:
    1. for ( int i = 0; i < model.rowCount(); ++i )
    2. tableView.openPersistentEditor( model.index(i, 1, QModelIndex()) );
    To copy to clipboard, switch view to plain text mode 

    When it starts though, the combobox show a blank value. I tried setCurrentIndex(1) but it is totally ignored:


    Qt Code:
    1. QWidget *ComboBoxDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem &/* option */,
    2. const QModelIndex &/* index */) const
    3. {
    4. QComboBox *editor = new QComboBox(parent);
    5. editor->addItem("a");
    6. editor->addItem("b56");
    7. editor->addItem("cc");
    8.  
    9. editor->setCurrentIndex(1);
    10. return editor;
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: setEditorData() for QComboBox Delegate

    Set the index in setEditorData().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setEditorData() for QComboBox Delegate

    Do you mean this?

    Qt Code:
    1. void ComboBoxDelegate::setEditorData(QWidget *editor,
    2. const QModelIndex &index) const
    3. {
    4. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    5. comboBox->setCurrentIndex( 1 );
    6. // comboBox->setCurrentIndex(comboBox->findText(index.data().toString()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    In this way the combo box will not show any changes that occur in the database..it will always show index 1.
    Is there a way to do it in the createEditor function?

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

    Default Re: setEditorData() for QComboBox Delegate

    Uncomment line #6 in your code and change the findText() part to something that detects if the string in the model is present in the combobox before you blindly set it on the combo.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Nov 2010
    Posts
    142
    Thanks
    24
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: setEditorData() for QComboBox Delegate

    Uncomment line #6 in your code and change the findText() part to something that detects if the string in the model is present in the combobox before you blindly set it on the combo.
    I am sorry but I did not understand your correction.
    The combo has three items:

    Qt Code:
    1. editor->addItem("a");
    2. editor->addItem("b56");
    3. editor->addItem("cc");
    To copy to clipboard, switch view to plain text mode 

    When the program starts running I would like the combo to show the first item "a" instead of blank.
    Then the user can choose any item s/he wishes.

    Is there a way to initialize the combo to item "a"?

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

    Default Re: setEditorData() for QComboBox Delegate

    The combobox has to show whatever data is in the model for the current cell and not what you want. You can say that if the model doesn't return data for an index, "a" should be set as the combobox current item. But then the model will never contain "a" (or its equivalent) unless you first change the combobox to something else, close the editor, reopen it again and change the cell back to "a".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Feb 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: setEditorData() for QComboBox Delegate

    I have been working on the same thing. Like wysota said, the combobox has to show whatever data is in the model for the current cell. to initialise the combobox to 'a', set the value in the model to 'a'. Then in the setEditorData() function you can retrieve the value in the model using something like this.

    Qt Code:
    1. QString value = index.model()->data(index, Qt::DisplayRole).toString();
    To copy to clipboard, switch view to plain text mode 

    Then you can set the current index like you have previously.

    Qt Code:
    1. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    2. comboBox->setCurrentIndex(comboBox->findText(value));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 2
    Last Post: 29th December 2012, 14:14
  2. Need help with my delegate
    By Guilo in forum Qt Programming
    Replies: 6
    Last Post: 1st July 2010, 12:15
  3. QCombobox as QTableView delegate
    By martonmiklos in forum Qt Programming
    Replies: 7
    Last Post: 13th June 2010, 15:23
  4. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 13:40
  5. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28

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.