Results 1 to 12 of 12

Thread: Editable QComboBox with QItemDelegate

  1. #1
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Editable QComboBox with QItemDelegate

    Hi all

    I am trying to customize my QTreeView to get an editable QComboBox into a column.
    There are some example on the Net, but I haven't seen any with an editable QComboBox, and mine does not work properly...

    Let me explain : if I click into the cell and select an item from the list it works : the selection is saved. But if I enter free text and change the focus, then the text is lost, and my DisplayRole role contains nothing. But if I enter some text again, then it will be saved...

    I searched without success some hours but didn't find any clue, so I'm posting here, maybe someone has a good idea about this...

    Now, some code :
    Qt Code:
    1. QWidget *ActionDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QComboBox *comboBox = new QComboBox(parent);
    4. comboBox->setEditable(true);
    5. comboBox->setInsertPolicy(QComboBox::NoInsert);
    6. comboBox->addItems(index.model()->data(index, CustomRoles::ValueRole).toStringList());
    7.  
    8. connect(comboBox, SIGNAL(activated(int)), this, SLOT(emitCommitData()));
    9. connect(comboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(emitCommitData()));
    10.  
    11. QString currentText = index.model()->data(index, Qt::DisplayRole).toString();
    12. int selectedItem = comboBox->findText(currentText);
    13. if(selectedItem == -1)
    14. comboBox->setEditText(index.model()->data(index, Qt::DisplayRole).toString());
    15. else
    16. comboBox->setCurrentIndex(selectedItem);
    17.  
    18. return comboBox;
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ActionDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    4.  
    5. comboBox->clear();
    6. comboBox->addItems(index.model()->data(index, CustomRoles::ValueRole).toStringList());
    7.  
    8. QString currentText = index.model()->data(index, Qt::DisplayRole).toString();
    9. int selectedItem = comboBox->findText(currentText);
    10. if(selectedItem == -1)
    11. comboBox->setEditText(index.model()->data(index, Qt::DisplayRole).toString());
    12. else
    13. comboBox->setCurrentIndex(selectedItem);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ActionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    2. {
    3. QComboBox* comboBox = qobject_cast<QComboBox*>(editor);
    4.  
    5. QStringList value;
    6. for(int i=0;i<comboBox->count();++i)
    7. value.append(comboBox->itemText(i));
    8.  
    9. int selectedItem = comboBox->findText(comboBox->currentText());
    10. if(selectedItem == -1)
    11. {
    12. model->setData(index, -1, CustomRoles::SelectedItemRole);
    13. model->setData(index, comboBox->currentText(), Qt::DisplayRole);
    14. }
    15. else
    16. {
    17. model->setData(index, selectedItem, CustomRoles::SelectedItemRole);
    18. model->setData(index, comboBox->itemText(selectedItem), Qt::DisplayRole);
    19. }
    20.  
    21. model->setData(index, value, CustomRoles::ValueRole);
    22. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void ActionDelegate::emitCommitData()
    2. {
    3. emit commitData(qobject_cast<QWidget *>(sender()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    CustomRoles::SelectedItemRole represents the currently selected item, or -1 if it's free text
    CustomRoles::ValueRole represents all the items of the combo box
    Last edited by Jmgr; 8th July 2008 at 21:37.
    Coding for Actionaz 3

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Editable QComboBox with QItemDelegate

    You have to play a bit with event filters, so that for example when you focus out of the editor or when you press the enter key, the contents is saved into the model.

  3. #3
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    But why does
    Qt Code:
    1. connect(comboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(emitCommitData()));
    To copy to clipboard, switch view to plain text mode 
    not save the data from the QLineEdit ?

    I also noticed that setEditorData is called twice after an edition...
    Coding for Actionaz 3

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Editable QComboBox with QItemDelegate

    What is emitCommitData()? Is it a slot? The line edit is not the editor widget, but part of it, so your emitCommitData() doesn't make much sense.

  5. #5
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    Well, as I posted on the first post it's a slot which calls
    Qt Code:
    1. emit commitData(qobject_cast<QWidget *>(sender()));
    To copy to clipboard, switch view to plain text mode 

    EDIT : Yes, it's a slot
    Coding for Actionaz 3

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Editable QComboBox with QItemDelegate

    Yeah, but it doesn't make much sense, as the line edit is not the editor.

  7. #7
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    Huh, that is true

    Ok, I will look how to use the event filter.
    Coding for Actionaz 3

  8. #8
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    It is probably a noobish question, but how do I installEventFilter within createEditor, because since createEditor is const, calling myLineEdit->installEventFilter(this) produces an error...

    I doesn't know why it is so complicated to put a simple QComboBox into a cell

    Anyway, I tested with having persistent editors : it seems to work, but the method of showing an editor when needed is much better...
    Coding for Actionaz 3

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Editable QComboBox with QItemDelegate

    You have to cheat using const_cast(). You can take a look at the implementation of QItemDelegate and the factory class it uses to see how it is internally implemented. With all the event filters and stuff.

  10. #10
    Join Date
    Jul 2008
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    I will look at it, thanks
    Coding for Actionaz 3

  11. #11
    Join Date
    Dec 2008
    Location
    Irkutsk, Russia
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Editable QComboBox with QItemDelegate

    Excuse me, may be it's easy, but what is CustomRoles?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: Editable QComboBox with QItemDelegate

    Quote Originally Posted by faraslacks View Post
    Excuse me, may be it's easy, but what is CustomRoles?
    [WIKI=QAbstractItemModel#Custom_item_roles]Custom item roles in ItemViews[/WIKI]

Similar Threads

  1. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 20:25

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.