mapdelegate.cpp

来自「Qt4源代码,详细介绍了Qt4编程中的范例」· C++ 代码 · 共 40 行

CPP
40
字号
#include <QtGui>

#include "mapdelegate.h"


MapDelegate::MapDelegate(QObject *parent)
    : QItemDelegate(parent)
{
}

void MapDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex& index) const
{
	if(index.column() == 0)	// company
	{
        QComboBox *comboEditor = qobject_cast<QComboBox *>(editor);
        if (comboEditor) {
			int i = comboEditor->findText(index.model()->data(index, Qt::EditRole).toString());
			comboEditor->setCurrentIndex(i);
        }		
	}
	else
		return QItemDelegate::setEditorData(editor, index);
}

void MapDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex& index) const
{
	if(index.column() == 0)
	{
		QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
		if(comboBox)
		{
			model->setData(index, comboBox->currentText());
		}
	}
	else
		return QItemDelegate::setModelData(editor, model, index);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?