2009-05-12 6 views
3

모델/뷰/컨트롤러 패러다임을 따랐습니다. 나는 모델과 뷰가 옳다는 것을 확신하지만, 나는 나의 대의원에게 잘못된 것을하고 있다고 생각한다. 모든 것은 "작동"합니다. 단, 컨트롤을 처음 클릭하는 것만 제외하면 "컨트롤이 켜지 며"두 번째 컨트롤과 상호 작용합니다. 이것은 대표자가 일반적으로 구현되는 방식입니까? 내 구현은 scoped_ptr에 의해 감추어 진 많은 구조와 파괴를 필요로하므로 그 어떤 팁도 도움이된다.Qt에서 대리자를 어떻게 올바르게 구현합니까?

QWidget *ParmDelegate::createWidget(const QModelIndex &index) const { 
    if (!index.isValid()) 
     return NULL; 
    const Parm *p = static_cast<const Parm*>(index.internalPointer()); 
    QWidget *w = p->createControl(); 
    w->setAutoFillBackground(true); 
    w->setBackgroundRole(QPalette::Base); // white background instead of grey 
    return w; 
} 

QWidget* 
ParmDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { 
    QWidget *retval = createWidget(index); 
    if (dynamic_cast<QComboBox*>(retval)) 
     connect(retval, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor())); 
    else if (dynamic_cast<QSlider*>(retval)) 
     connect(retval, SIGNAL(sliderReleased()), this, SLOT(commitAndCloseEditor())); 
    else if (dynamic_cast<QAbstractButton*>(retval)) 
     connect(retval, SIGNAL(clicked()), this, SLOT(commitAndCloseEditor())); 
    else 
     connect(retval, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor())); 
    retval->setFocusPolicy(Qt::StrongFocus); 
    retval->setParent(parent); 
    return retval; 
} 

void 
ParmDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { 
    const Parm *p = static_cast<const Parm*>(index.internalPointer()); 
    p->setEditorData(editor); 
} 

void 
ParmDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { 
    ParmControl::Base* base = dynamic_cast<ParmControl::Base*>(editor); 
    model->setData(index, base->toQVariant()); 
} 

void 
ParmDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { 
    editor->setGeometry(option.rect); 
} 

void 
ParmDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { 
    scoped_ptr<QWidget> w(createWidget(index)); 
    if (!w) 
     return; 
    const Parm *p = static_cast<const Parm*>(index.internalPointer()); 
    setEditorData(w.get(), index); 
    w->setGeometry(option.rect); 
    w->render(painter, option.rect.topLeft()); 
} 

QSize 
ParmDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { 
    scoped_ptr<QWidget> w(createWidget(index)); 
    if (!w) 
     return QSize(0,0); 
    return w->sizeHint(); 
} 

void 
ParmDelegate::commitAndCloseEditor() { 
    QWidget *editor = static_cast<QWidget *>(sender()); 
    ParmControl::Base* base = dynamic_cast<ParmControl::Base*>(editor); 
    emit commitData(editor); 
    emit closeEditor(editor, QAbstractItemDelegate::EditNextItem); 
} 
+0

아마도 Model-View-Controller를 의미 할 것입니다. 모델/뷰/위임 패러다임과 같은 것은 없다. – shoosh

+0

Fixed .......... –

답변

4

사용자 지정 편집기가 표시되는 조건을 변경하려면 QAbstractItemView::setEditTriggers()을 사용하십시오. 대리자가 사용자 지정 편집기에서 정보를 전달하는 역할을 담당하지만보기에서는 편집기를 시작할시기를 결정합니다.

문서 참조 번호 : http://doc.qt.digia.com/4.5/qabstractitemview.html#editTriggers-prop.

+0

시도했지만 첫 번째 클릭에 대한 편집 트리거가 없습니다. SelectedClicked와 DoubleClicked 만 있으므로 editorEvent를 사용하여 첫 번째 클릭을 강제로 편집 할 것이라고 생각했습니다. 잘못된 방법? –

+1

CurrentChanged를 시도해보십시오. 셀 선택이 변경되면 편집이 시작됩니다. – swongu

+0

와우, 선택이 비활성화되어 있어도 작동합니다. 편집기 이벤트 메서드가 제거됩니다. 나는 위의 코드에서도 그것을 제거 할 것이다. –

관련 문제