In the code data model class code is as follows:
class FormulaListModel: public QAbstractTableModel
{
Typedef struct
{
QString op1_name;
QString op2_name;
QString op;
Bool isEnable;
} FORMULA, * PFORMULA;
Q_OBJECT
Public:
FormulaListModel (QObject * parent);
~ FormulaListModel ();
/* Implement the necessary virtual function of QAbstractListModel */
Int the rowCount (const QModelIndex & amp; The parent=QModelIndex const ());
Int columnCount (const QModelIndex & amp; The parent=QModelIndex const ());
QVariant data (const QModelIndex & amp; The index, int role=Qt: : DisplayRole) const;
Private:
QVectorM_lsFormula;
};
FormulaListModel: : FormulaListModel (QObject * parent)
: QAbstractTableModel (parent)
{
int i;
For (I=0; I & lt; 40; I + +)
{
PFORMULA formula=new formula;
Formula - & gt; IsEnable=true;
Formula - & gt; Op="+";
Formula - & gt; Op1_name=QString (" % 1 "). Arg (I);
Formula - & gt; Op2_name=QString (" % 1 "). Arg (50 - I);
M_lsFormula. Append (formula);
}
}
FormulaListModel: : ~ FormulaListModel ()
{
Foreach (PFORMULA f, m_lsFormula)
{
If (f)
{
Delete the f;
}
}
M_lsFormula. The clear ();
}
Int FormulaListModel: : rowCount (const QModelIndex & amp; The parent) const
{
Return m_lsFormula. The count ();
}
Int FormulaListModel: : columnCount (const QModelIndex & amp; The parent) const
{
The return of 4;
}
QVariant FormulaListModel: : data (const QModelIndex & amp; Const index, int role)
{
The switch (role)
{
Case Qt: : EditRole:
{
Int r=index. The row ();
C=int index. The column ();
PFORMULA pf=m_lsFormula [r].
The switch (c)
{
Case 0:
Return pf - & gt; IsEnable;
Case 1:
Return pf - & gt; Op1_name;
Case 2:
Return pf - & gt; The op.
Case 3:
Return pf - & gt; Op2_name;
}
}
Case Qt: : DisplayRole:
{
C=int index. The column ();
Int r=index. The row ();
PFORMULA pf=m_lsFormula [r].
The QStringList lsStr;
The switch (c)
{
Case 0:
Return "IsUsed";
Case 1:
Case 3:
LsStr & lt; & lt; "1";
LsStr & lt; & lt; "2";
Return lsStr;
Case 2:
LsStr & lt; & lt; "+".
LsStr & lt; & lt; "-";
Return lsStr;
}
}
Case Qt: : DecorationRole:
{
QColor CLR (Qt: : transparent);
Return the CLR.
}
Case Qt: : SizeHintRole:
Return QSize (130, 16);
Case Qt: : FontRole:
{
QFont font;
The font. SetBold (true);
Return the font;
}
Case Qt: : BackgroundRole:
{
QBrush brush (Qt: : transparent);
Return the brush;
}
Case Qt: : TextAlignmentRole:
{
Return Qt: : AlignHCenter;
}
Case Qt: : CheckStateRole:
{
Return Qt: : Checked;
}
Case Qt: : TextColorRole:
{
QColor CLR (Qt: : black);
Return the CLR.
}
Case Qt: : ToolTipRole:
{
Return "Formula List";
}
}
}
Table cell proxy class code is as follows:
class CheckBoxDelegate: public QStyledItemDelegate
{
Q_OBJECT
Public:
CheckBoxDelegate (QObject * parent);
~ CheckBoxDelegate ();
/* * * * * if you use a proxy instead of editor factory to provide the editor, the virtual function of the following must be overloaded * * * * * * * */
QWidget * createEditor (QWidget * parent, const QStyleOptionViewItem & amp; Option,
Const QModelIndex & amp; The index) const;
Void setEditorData (QWidget * editor, const QModelIndex & amp; The index) const;
Void updateEditorGeometry (QWidget * editor,
Const QStyleOptionViewItem & amp; Option,
Const QModelIndex & amp; The index) const;
Void setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & amp; The index) const;
Void paint (QPainter * painter, const QStyleOptionViewItem & amp; Option, const QModelIndex & amp; The index) const;
Protected:
Private:
};
CheckBoxDelegate: : CheckBoxDelegate (QObject * parent)
: QStyledItemDelegate (parent)
{
}
CheckBoxDelegate: : ~ CheckBoxDelegate ()
{
}
* CheckBoxDelegate QWidget: : createEditor (QWidget * parent, const QStyleOptionViewItem & amp; Option,
Const QModelIndex & amp; Const index)
{
QVariant value=(https://bbs.csdn.net/topics/index.model) -> data (index, Qt: : DisplayRole);
If (index. The column ()==0)
{
PBox QCheckBox *=new QCheckBox (parent);
PBox - & gt; SetText (value. The toString ());
Return pBox;
}
The else
{
PBox QComboBox *=new QComboBox (parent);
PBox - & gt; AddItems (value. ToStringList ());
Return pBox;
}
}
Void CheckBoxDelegate: : setEditorData (QWidget * editor, const QModelIndex & amp; Const index)
{
QVariant value=(https://bbs.csdn.net/topics/index.model) -> data (index, Qt: : EditRole);
If (index. The column ()==0)
{
Bool checked=value. ToBool ();
PBox QCheckBox *=static_cast & lt; QCheckBox * & gt; (editor); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull