2013-07-10 42 views
0

的文件似乎说这个插孔,我已经看到了一堆周围的StackOverflow等地暧昧的示例代码,所以......Qt信号转发;继承QAbstractProxyModel

如果我有一个类A实现了一个QAbstractProxyModel和类B,实现QAbstractItemModel,我呼吁A方法的实例setSourceModel(b)其中bB的实例,是否会自动照顾转发更新信号,如modelReset,rowsInserted等。或者我必须手动连接所有这些?

回答

0

如果类似class A : public QAbstractProxyModelclass B : public QAbstractItemModel那么信号和时隙也应该被继承。 (除非你想

它特殊的行为如果“QAbstractClasses”是A简单成员和B你有“前进”他们

+0

该信号被继承,并不意味着他们将正常工作的事实。例如,如果您子类'QAbstractItemModel',则必须在'setData'方法实现中发出'dataChanged'信号。 –

0

从文档:

要继承QAbstractProxyModel,你需要实现mapFromSource()和mapToSource()。如果你需要一个不同于默认行为的行为,mapSelectionFromSource()和mapSelectionToSource()函数只需要重新实现。

没有关于信号的词。所以它在所提到的方法的文档中。这意味着你不需要关心信号,它们会自动发射。

1

如果class A : public QAbstractProxyModel,class B : public QAbstractItemModel某些信号转发和更新良好(dataChanged等)。但有些(如rowsInserted)需要手动更正。

我用这样的代码:

... 
#define COL_ID 0 

void A::setSourceModel(QAbstractItemModel *newSourceModel) { 
    beginResetModel(); 
    if (this->sourceModel()) { // disconnect sourceModel signals 
     ... 
    } 
    ... 
    QAbstractProxyModel::setSourceModel(newSourceModel); 
    if (this->sourceModel()) { // connect sourceModel signals 
     ... 
     connect(this->sourceModel(), SIGNAL(rowsInserted(QModelIndex,int,int)), 
      this, SLOT(sourceRowsInserted(QModelIndex, int, int))); 
     ... 
    } 
    return; 
} 
... 
void A::sourceRowsInserted(const QModelIndex &parent, int first, int last) { 
    QModelIndex parentIndex = this->mapFromSource(parent); 
    QModelIndex sourceTopIndex = this->sourceModel()->index(first, COL_ID, parent); 
    QModelIndex sourceBottomIndex = this->sourceModel()->index(last, COL_ID, parent); 
    QModelIndex topIndex = this->mapFromSource(sourceTopIndex); 
    QModelIndex bottomIndex = this->mapFromSource(sourceBottomIndex); 
    beginInsertRows(parentIndex, topIndex.row(), bottomIndex.row()); 
    endInsertRows(); 
    return; 
} 

...