Results 1 to 12 of 12

Thread: Editable QComboBox with QItemDelegate

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 20:37.
    Coding for Actionaz 3

Similar Threads

  1. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19: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.