Delegate not work



  • hi,all
    i am a newbie.i used delegate in tabview,but it cannot work properly.
    @ view = new QTableView(this);
    QStringList list;
    list<<tr( "item1" )<<tr( "item2" )<<tr( "item3" )<<tr( "item4" );
    viewModel = new QStandardItemModel(0,list.count(),this);
    view->setModel( viewModel );
    ComboxDelegate comboBoxDelegate1;
    view->setItemDelegateForColumn(3, &comboBoxDelegate1);@

    my delegate:
    @class ComboxDelegate : public QItemDelegate
    {
    Q_OBJECT
    public:
    ComboxDelegate(QObject *parent = 0): QItemDelegate(parent) { }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    const QModelIndex &index) const
    {
    QComboBox *editor = new QComboBox(parent);
    editor->addItem("Female");
    editor->addItem("Male");
    return editor;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const
    {
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QComboBox comboBox = static_cast<QComboBox>(editor);
    int tindex = comboBox->findText(text);
    comboBox->setCurrentIndex(tindex);
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model,
    const QModelIndex &index) const
    {
    QComboBox comboBox = static_cast<QComboBox>(editor);
    QString text = comboBox->currentText();
    model->setData(index, text, Qt::EditRole);
    }
    void updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
    editor->setGeometry(option.rect);
    }
    };@


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to explain what doesn't work.

    From a quick overview:

    @
    ComboxDelegate comboBoxDelegate1;
    view->setItemDelegateForColumn(3, &comboBoxDelegate1);
    @

    comboBoxDelegate1 will be destroyed shortly after that so it won't be used.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.