2013-03-21 2 views
0

Qt의 스핀 상자 대리인 예제에서 복사 한 대리자를 작성했으며 QTableView를 채우려고합니다. 그러나 테이블 머리글이 나타나지만 셀이 비어있어 클릭 할 수없는 이상한 문제가 발생합니다.QTableView의 셀은 비어 있지만 헤더가 표시됩니다.

Problem with QTableView and delegate

대리자에 대한 코드 :

#include "double_spinbox_delegate.h" 

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

QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{ 
    QDoubleSpinBox *editor = new QDoubleSpinBox(parent); 
    editor->setValue(0); 

// if (index.column() == 0){ 
//  editor->setMinimum(0); 
//  editor->setMaximum(255); 
// } 
// else{ 
//  editor->setMinimum(0); 
//  editor->setMaximum(1); 
// } 

    return editor; 
} 

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

    QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor); 
    doubleSpinBox->setValue(value); 
} 

void DoubleSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{ 
    QDoubleSpinBox *doubleSpinBox = static_cast<QDoubleSpinBox*>(editor); 
    doubleSpinBox->interpretText(); 
    double value = doubleSpinBox->value(); 

    model->setData(index, value, Qt::EditRole); 
} 

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

내가

void MainWindow::InitializeColorTable(){ 
    QTableView *tableColor = ui->tableColor; 

    QStandardItemModel *model = new QStandardItemModel(4, 4, ui->tableColor); 
    // QStandardItemModel *model = this->colorTableModel; 
    tableColor->setModel(model); 

    DoubleSpinBoxDelegate delegate; 
    tableColor->setItemDelegate(&delegate); 

    model->setHorizontalHeaderLabels(QStringList() << tr("Value") << tr("R") << tr("G") << tr("B")); 

    for (int row = 0; row < model->rowCount(); ++row){ 
     for (int col = 0; col < model->columnCount(); ++col){ 
      QModelIndex index = model->index(row, col, QModelIndex()); 
      model->setData(index, QVariant((row + 1.0) * (col + 1.0)), Qt::EditRole); 
     } 
    } 
} 

답변

2

귀하의 대리인이 스택에 할당되는 형태의 생성자에서 전화 드렸습니다 기능, 그것은이다 범위를 벗어나면 삭제됩니다.

DoubleSpinBoxDelegate delegate; 
tableColor->setItemDelegate(&delegate); 

새로운 대신 대리인을 만드십시오.

+0

고마워요! 나는 그것을 확인하기 위해 새로운 눈 쌍이 필요한 뭔가가 될 것이라는 것을 알고있었습니다. –

관련 문제