당신은 QDataWidgetMapper::setItemDelegate
를 사용하여 남녀 모델의 열을 처리 할 QItemDelegate
파생 클래스를 작성할 수 있습니다
void ItemDelegate::setEditorData (QWidget * editor, const QModelIndex & index) const {
if(index.column() == GenderColumnIndex) {
QComboBox *combobox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combobox);
if(index.data().toString() == "M") {
combobox->setCurrentIndex(0);
} else {
combobox->setCurrentIndex(1);
}
} else {
QItemDelegate::setEditorData(editor, index);
}
}
void ItemDelegate::setModelData (QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const {
if(index.column() == GenderColumnIndex) {
QComboBox *combobox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combobox);
if(combobox->currentIndex() == 0) {
model->setData(index, "M");
} else {
model->setData(index, "F");
}
} else {
QItemDelegate::setModelData(editor, model, index);
}
}
또는
당신은 QComboBox
파생 클래스를 작성하고 그 QDataWidgetMapper
할 수있는 사용자 지정 속성을 정의 할 수 있습니다 성별 편지 읽기/쓰기에 사용 :
class QGenderComboBox : public QComboBox
{
Q_OBJECT
// If you set USER to true, you can omit the propertyName parameter
// when you call QDataWidgetMapper::addMapping
Q_PROPERTY(QString value READ value WRITE setValue USER true)
public:
QGenderComboBox(QWidget *parent);
// Sets the currentIndex from the gender letter
void setValue(const QString);
// Returns the letter from the currentIndex
QString value() const;
};