清源游民 gameogre@gmail.com
Delegate 類
概念
與MVC模式不同,model/view結(jié)構(gòu)沒有用于與用戶交互的完全獨(dú)立的組件。一般來講, view負(fù)責(zé)把數(shù)據(jù)展示
給用戶,也處理用戶的輸入。為了獲得更多的靈性性,交互通過delegagte執(zhí)行。它既提供輸入功能又負(fù)責(zé)渲染view中的每個(gè)數(shù)據(jù)項(xiàng)。 控制delegates的標(biāo)準(zhǔn)接口在QAbstractItemDelegate類中定義。Delegates通過實(shí)現(xiàn)paint()和sizeHint()以達(dá)到渲染內(nèi)容的目的。然而,簡(jiǎn)單的基于widget的delegates,可以從QItemDelegate子類化,而不是QAbstractItemDelegate,這樣可以使用它提供的上述函數(shù)的缺省實(shí)現(xiàn)。delegate可以使用widget來處理編輯過程,也可以直接對(duì)事件進(jìn)行處理。
使用現(xiàn)成的delegate
Qt提供的標(biāo)準(zhǔn)views都使用QItemDelegate的實(shí)例來提供編輯功能。它以普通的風(fēng)格來為每個(gè)標(biāo)準(zhǔn)view渲染數(shù)據(jù)項(xiàng)。這些標(biāo)準(zhǔn)的views包括:QListView,QTableView,QTreeView。所有標(biāo)準(zhǔn)的角色都通過標(biāo)準(zhǔn)views包含的缺省delegate進(jìn)行處理。一個(gè)view使用的delegate可以用itemDelegate()函數(shù)取得,而setItemDelegate() 函數(shù)可以安裝一個(gè)定制delegate。
一個(gè)簡(jiǎn)單的delegate
這個(gè)delegate使用QSpinBox來提供編輯功能。它主要想用于顯示整數(shù)的models上。盡管我們已經(jīng)建立了一個(gè)基于整數(shù)的table model,但我們也可以使用QStandardItemModel,因?yàn)閐elegate可以控制數(shù)據(jù)的錄入。我們又建了一個(gè)table view來顯示model的內(nèi)容,用我們定制的delegate來編輯。

我們從QItemDelegate子類化,這樣可以利用它缺省實(shí)現(xiàn)的顯示功能。當(dāng)然我們必需提供函數(shù)來管理用于編輯的widget:
class SpinBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
SpinBoxDelegate(QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
void setEditorData(QWidget *editor, const QModelIndex &index) const;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const;
void updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
需要注意的是,當(dāng)一個(gè)delegate創(chuàng)建時(shí),不需要安裝一個(gè)widget,只有在真正需要時(shí)才創(chuàng)建這個(gè)用于編輯的widget。
提供編輯器
在這個(gè)例子中,當(dāng)table view需要提供一個(gè)編輯器時(shí),它要求delegate提供一個(gè)可用于編輯的widget,它應(yīng)該適用于當(dāng)前正被修改的數(shù)據(jù)項(xiàng)。這正是createEditor()函數(shù)應(yīng)該實(shí)現(xiàn)的:
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
我們不需要跟蹤這個(gè)widget的指針,因?yàn)関iew會(huì)在不需要時(shí)銷毀這個(gè)widget。我們也給編輯安裝了delegate缺省的事件過濾器,這提供了用戶期望的標(biāo)準(zhǔn)編輯快捷鍵。view通過我們定義相應(yīng)的函數(shù)來保證編輯器的數(shù)據(jù)與幾何布局被正確的設(shè)置。我們也可以根據(jù)不同的model index來創(chuàng)建不同的編輯器,比如,我們有一列整數(shù),一列字符串,我們可以根據(jù)哪種列被編輯來創(chuàng)建一個(gè)QSpinBox或是QLineEdit。delegate必需提供一個(gè)函數(shù)把model中的數(shù)據(jù)拷貝到編輯器中。
void SpinBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
向model提交數(shù)據(jù)
這需要我們實(shí)現(xiàn)另外一個(gè)函數(shù)setModelData():
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value);
}
標(biāo)準(zhǔn)的QItemDelegate類當(dāng)它完成編輯時(shí)會(huì)發(fā)射closeEditor()信號(hào)來通知view。view保證編輯器widget關(guān)閉與銷毀。本例中我們只提供簡(jiǎn)單的編輯功能,因此不需要發(fā)送個(gè)信號(hào)。
更新編輯器幾何布局
delegate負(fù)責(zé)管理編輯器的幾何布局。這些幾何布局信息在編輯創(chuàng)建時(shí)或view的尺寸位置發(fā)生改變時(shí),
都應(yīng)當(dāng)被提供。幸運(yùn)的是,view通過一個(gè)view option可以提供這些必要的信息。
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
編輯提示
編輯完成后,delegate會(huì)給別的組件提供有關(guān)于編輯處理結(jié)果的提示,也提供用于后續(xù)編輯操作的一些提示。
這可以通過發(fā)射帶有某種hint的closeEditor()信號(hào)完成。這些信號(hào)會(huì)被安裝在spin box上的缺省的QItemDelegate事件過濾器捕獲。對(duì)這個(gè)缺省的事件過濾來講,當(dāng)用戶按下回車鍵,delegate會(huì)對(duì)model中的數(shù)據(jù)進(jìn)行提交,并關(guān)閉spin box。
我們可以安裝自己的事件過濾器以迎合我們的需要,例如,我們可以發(fā)射帶有EditNextItem hint的
closeEditor()信號(hào)來實(shí)現(xiàn)自動(dòng)開始編輯view中的下一項(xiàng)。
posted on 2007-06-19 17:37
清源游民 閱讀(6893)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
Qt