Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Item Delegates QStringList and QComboBox

Item Delegates QStringList and QComboBox

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hec_mex
    wrote on last edited by
    #1

    Hello!

    I'm implementing an item delegate to extend the QStyledItemDelegate. The idea is whe an value is a QStringList i use a QComboBox as editor in the item delegate. But i have some problems

    1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn't show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell

    2. When i want to know what is the selection i can't figure ir out how to get the item selected in the QComboBox.

    Any ideas?

    @
    class ItemDelegate : public QStyledItemDelegate {
    Q_OBJECT

    public:
    ItemDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {;}
    virtual ~ItemDelegate() { ; }

     QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                           const QModelIndex &index) const;
    
     void setEditorData(QWidget *editor, const QModelIndex &index) const;
    
     void setModelData(QWidget *editor, QAbstractItemModel *model,
                       const QModelIndex &index) const;
    
     inline void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex& ) const {
       editor->setGeometry(option.rect);
     }
    

    };
    @

    @
    QWidget* ItemDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem & option ,
    const QModelIndex & index ) const {

    QVariant data = index.model()->data(index, Qt::EditRole);

    QWidget* editor;
    switch( data.type() ) {
    case QVariant::StringList:
    editor = new QComboBox(parent);
    dynamic_cast<QComboBox*>(editor)->setFrame(false);
    break;
    default:
    editor = QStyledItemDelegate::createEditor(parent, option, index);
    }

    return editor;
    }

    void ItemDelegate::setEditorData(QWidget *editor,
    const QModelIndex &index) const {

    QVariant data = index.model()->data(index, Qt::EditRole);

    switch( data.type() ) {
    case QVariant::StringList:
    static_cast<QComboBox*>(editor)->addItems(data.toStringList());
    break;
    default:
    QStyledItemDelegate::setEditorData(editor, index);
    }
    }

    void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    QVariant data = index.model()->data(index, Qt::EditRole);

    switch( data.type() ) {
    case QVariant::StringList: {
    QStringList list;
    for( int i = 0; i < static_cast<QComboBox*>(editor)->count(); ++i )
    list << static_cast<QComboBox*>(editor)->itemText(i);
    model->setData(index, QVariant(list), Qt::EditRole);
    break;
    }
    default:
    QStyledItemDelegate::setModelData(editor, model, index);
    }
    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved