how can i get Delegate for QTableWidget?
<p>Problem in getting delegates on QTableWidget, I'm trying to use two delegates (comboboxdelegate, spinbox delegate) to control user input in a QTableWidget with 2 columns and one row, but after reading the Delegate Classes page and looking through the SpinBox &amp; comboox Delegate example, I thought I'd figured out how things work, but clearly haven't.</p>

<p>(sorry for my english), any help is appreciated.</p>

<p>The code is as follows.</p>

<p>notepad.h file</p>

<pre><code>#ifndef NOTEPAD_H
#define NOTEPAD_H
#include &lt;QtGui&gt;

class Notepad : public QMainWindow
{
    Q_OBJECT

   public:
        //Notepad(QWidget *parent = 0, Qt::WFlags flags = 0);
        Notepad();
        void test();
        QTableWidget* table ;
    public slots:
        void add();
        void Delete();
        void mouseReleaseEvent ( QMouseEvent * event );

    private:
        QAction *add_action;
        QAction *Delete_action;

};

class SpinBoxDelegate : public QStyledItemDelegate,public Notepad
{
    Q_OBJECT
public:
    SpinBoxDelegate(QObject *parent = 0);
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option,
                          const QModelIndex &amp;index) /*const Q_DECL_OVERRIDE*/;

    void setEditorData(QWidget *editor, const QModelIndex &amp;index) /*const Q_DECL_OVERRIDE*/;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &amp;index) /*const Q_DECL_OVERRIDE*/;

    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) /*const Q_DECL_OVERRIDE*/;
};

class ComboBoxDelegate : public QStyledItemDelegate,public Notepad
{
Q_OBJECT

public:
ComboBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &amp;option,
const QModelIndex &amp;index) const;

void setEditorData(QWidget *editor, const QModelIndex &amp;index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &amp;index) const;

void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &amp;option, const QModelIndex &amp;index) const;
};

#endif // NOTEPAD_H
</code></pre>

<p>notepad.cpp file</p>

<pre><code>#include "notepad.h"
#include &lt;QMessageBox&gt;
#include &lt;QTableView&gt;
#include &lt;QMouseEvent&gt;
#include &lt;QSpinBox&gt;
#include &lt;QComboBox&gt;
#include &lt;QStandardItemModel&gt;
Notepad::Notepad()
{
    table = new QTableWidget();
     test() ;
        SpinBoxDelegate();
        ComboBoxDelegate();
    add_action = new QAction(tr("Add cell"), this);
    add_action-&gt;setIcon(QIcon("add.jpg"));
    Delete_action = new QAction(tr("Delete cell"), this);
    Delete_action-&gt;setIcon(QIcon("delete.jpg"));

    connect(Delete_action, SIGNAL(triggered()), this, SLOT(Delete()));
    connect(add_action, SIGNAL(triggered()), this, SLOT(add()));

    //tableItem-&gt;setFlags(tableItem-&gt;flags() ^ Qt::ItemIsEditable);

    centralWidget()-&gt;setAttribute(Qt::WA_TransparentForMouseEvents);
    centralWidget()-&gt;setAttribute(Qt::WA_MouseTracking,true);

    setMouseTracking(true);

}
void Notepad::test() 
{       

    QTableWidgetItem* tableItem = new QTableWidgetItem();

    table-&gt;setRowCount(1);
    table-&gt;setColumnCount(3);
    table-&gt;setItem(0,0,new QTableWidgetItem());
    table-&gt;setItem(0,1,new QTableWidgetItem());
    table-&gt;setItem(0,2,new QTableWidgetItem());

    table-&gt;setMouseTracking(true);
    table-&gt;viewport()-&gt;setMouseTracking(true);
    table-&gt;installEventFilter(this);
    table-&gt;viewport()-&gt;installEventFilter(this);

    table-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
    table-&gt;setSelectionMode(QAbstractItemView::SingleSelection);

    QStandardItemModel model((table-&gt;rowCount()),(table-&gt;columnCount()));
    SpinBoxDelegate sdelegate;

    ComboBoxDelegate comDel;
     for (int row = 0; row &lt; 4; ++row)
    {
        QWidget *parent;
        QStyleOptionViewItem option;
        for (int column = 0; column &lt; 1; ++column)
        {

            table-&gt;setItemDelegateForColumn( column, &amp;comDel);
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row + 1) * (column + 1)));
        }
        for (int column = 1; column &lt; 2; ++column) 
        {

        table-&gt;setItemDelegateForColumn( column, &amp;sdelegate  );
        QModelIndex index = model.index(row, column, QModelIndex());
        model.setData(index, QVariant((row + 1) * (column + 1)));
        }
    }




    table-&gt;setHorizontalHeaderItem(0, new QTableWidgetItem("combobox"));
    table-&gt;setHorizontalHeaderItem(1, new QTableWidgetItem("spinbox"));
    table-&gt;setHorizontalHeaderItem(2, new QTableWidgetItem("lineEdit"));
    tableItem-&gt;setFlags(tableItem-&gt;flags() ^ Qt::ItemIsEditable);
    table-&gt;setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    setCentralWidget(table);

}

void Notepad::mouseReleaseEvent (QMouseEvent * event )
{   
    QMessageBox* msgBox;
    if(event-&gt;button() == Qt::RightButton)
      {
QMouseEvent *mouseEvent = static_cast&lt;QMouseEvent*&gt; (event);
       QMenu *menu = new QMenu(this);
       menu-&gt;addAction(add_action);
       menu-&gt;addAction(Delete_action);
       menu-&gt;exec(mouseEvent-&gt;globalPos());
       //msgBox-&gt;setInformativeText("u pressed right button");            
    } 
}
void Notepad::add() 
{

    table-&gt;insertRow( 1);

    setCentralWidget(table);
    centralWidget()-&gt;setAttribute(Qt::WA_TransparentForMouseEvents);
    setMouseTracking(true);
}
void Notepad::Delete() 
{

    table-&gt;removeRow(1);

    setCentralWidget(table);
    centralWidget()-&gt;setAttribute(Qt::WA_TransparentForMouseEvents);
    setMouseTracking(true);
}








SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &amp;/* option */,
    const QModelIndex &amp;/* index */)/* const*/
{

    QSpinBox *editor = new QSpinBox(parent);
    editor-&gt;setFrame(false);
    editor-&gt;setMinimum(0);
    editor-&gt;setMaximum(100);
    return editor;
}

void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &amp;index) /*const*/
{
    int value = index.model()-&gt;data(index, Qt::EditRole).toInt();
    QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
    spinBox-&gt;setValue(value);
}

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &amp;index) /*const*/
{
    QSpinBox *spinBox = static_cast&lt;QSpinBox*&gt;(editor);
    spinBox-&gt;interpretText();
    int value = spinBox-&gt;value();
    model-&gt;setData(index, value, Qt::EditRole);
}

void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
    const QStyleOptionViewItem &amp;option, const QModelIndex &amp;/* index */) /*const*/
{
    editor-&gt;setGeometry(option.rect);
}

ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &amp;/* option */,
const QModelIndex &amp;/* index */) const
{
QComboBox *editor = new QComboBox(parent);
QStringList list ;
list &lt;&lt; "srikanth" &lt;&lt; "dilip";
editor-&gt;addItems(list);
//editor-&gt;installEventFilter(const_cast&lt;ComboBoxDelegate*&gt;(this));

return editor;
}

void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &amp;index) const
{
QString value = index.model()-&gt;data(index, Qt::DisplayRole).toString();

QComboBox *comboBox = static_cast&lt;QComboBox*&gt;(editor);
comboBox-&gt;addItem(value);
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &amp;index) const
{
QComboBox *comboBox = static_cast&lt;QComboBox*&gt;(editor);
QString value = comboBox-&gt;currentText();

model-&gt;setData(index, value);
}

void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &amp;option, const QModelIndex &amp;/* index */) const
{
editor-&gt;setGeometry(option.rect);
}
</code></pre>

<p>main.cpp file</p>

<pre><code>#include &lt;QtGui&gt;
#include &lt;notepad.h&gt;
int main(int argv, char **args)  {
    QApplication app(argv, args);

   Notepad notepad;

    notepad.show();

    return app.exec();
}
</code></pre>