2013-10-19 11 views
0

테이블의 특정 열에 스핀 상자 항목 대리자를 추가하려고합니다. Qt의 예제를 살펴본 후에 해당 코드의 대부분을 복사하고 구현했지만 setItemDelegateForColumn()으로 전화하면 응용 프로그램이 충돌합니다. 열 인덱스가 유효합니다. 내가 뭘 잘못했는지 생각해?Qt5 C++ : 특정 테이블 열에 대해 Spinbox 대리인을 설정하십시오.

홈페이지 호출 방법 :

BinarySpinboxDelegate binarySpinboxDelegate; 
ui->UsersTable->setItemDelegateForColumn(users->at(0).size()-1 ,&binarySpinboxDelegate); 

사용자 정의 Spinbox 구현 :

#include "binaryspinboxdelegate.h" 
#include <QSpinBox> 

BinarySpinboxDelegate::BinarySpinboxDelegate(QObject *parent) : QStyledItemDelegate(parent) 
{ 

} 

QWidget* BinarySpinboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const 
{ 
    QSpinBox* editor = new QSpinBox(parent); 
    editor->setFrame(false); 
    editor->setMinimum(0); 
    editor->setMaximum(1); 

    return editor; 
} 

void BinarySpinboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const 
{ 
    int value = index.model()->data(index, Qt::EditRole).toInt(); 

    QSpinBox *spinBox = static_cast<QSpinBox*>(editor); 
    spinBox->setValue(value); 
} 

void BinarySpinboxDelegate::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, Qt::EditRole); 
} 

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

답변

1

그리고 내가 바보가 -_- 내가이 초기화 함수 내에서 대리자를 선언 할만큼 바보였다 밝혀 함수가 완료된 후 범위를 벗어나는 테이블의 경우. 헤더 파일로 옮겨서 객체의 수명 동안 존재하므로 괜찮 았습니다.

+1

가비지 컬렉터가 처음으로 물었던 것도 아닙니다. 하지만 당신의 대답은 여전히 ​​나를 구 했어요. 저는 PySide에서 대표자를 편집하고 있습니다. – foresightyj

+0

내 문제가 다른 사람들에게 도움이 될 수있어 기쁘다. L : D – T3CHN0CR4T