2011-08-17 2 views
2

QAbstractItemModel 및 QItemDelegate가 있고 여기 내 문제가 있습니다. 대의원은 아무 것도하지 않습니다. 그 서브 루틴은 호출되고 있지만 아무 일도 일어나지 않습니다.QAbstractTableModel에서 QItemDelegate 사용

다음은 내 탁자에서보고 싶습니다.

텍스트 : QComboBox : 텍스트 : 텍스트 : QProgressBar

: 열 구분자입니다.

대리인. #ifndef의 DELEGATEACTION_H #DEFINE의 DELEGATEACTION_H

#include <QVariant> 
#include <QItemDelegate> 

#include <QWidget> 
#include <QLabel> 
#include <QComboBox> 
#include <QProgressBar> 

class DelegateAction : public QItemDelegate 
{ 
    Q_OBJECT 
public: 
    explicit DelegateAction(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; 
}; 

#endif // DELEGATEACTION_H 

#include "DelegateAction.h" 

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

QWidget * DelegateAction::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const 
{ 
    QWidget* editor = 0; 

    switch (index.column()) 
    { 
    case 0: 
    default: 
    { 
     editor = new QLabel(); 
     break; 
    } 
    case 1: 
    { 
     QComboBox* combo = new QComboBox(parent); 
     combo->addItem("Test"); 
     combo->addItem("Test 2"); 
     editor = combo; 
     break; 
    } 
    case 4: 
    { 
     editor = new QProgressBar(parent); 
     break; 
    } 
    } 

    editor->installEventFilter(const_cast<DelegateAction*>(this)); 
    return editor; 
} 

void DelegateAction::setEditorData(QWidget *editor, const QModelIndex &index) const 
{ 
    QVariant value = index.model()->data(index, Qt::DisplayRole); 

    switch (index.column()) 
    { 
    case 0: 
    default: 
    { 
     QLabel* label = static_cast<QLabel*>(editor); 
     label->setText(value.toString()); 
     break; 
    } 
    case 1: 
    { 
     QComboBox* combo = static_cast<QComboBox*>(editor); 
     combo->setCurrentIndex(combo->findText(value.toString())); 
     break; 
    } 
    case 4: 
    { 
     QProgressBar* progress = static_cast<QProgressBar*>(editor); 
     progress->setValue(value.toInt()); 
     break; 
    } 
    } 

} 

void DelegateAction::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const 
{ 
    QVariant value; 
    switch (index.column()) 
    { 
    case 0: 
    default: 
    { 
     value = static_cast<QLabel*>(editor)->text(); 
     break; 
    } 
    case 1: 
    { 
     value = static_cast<QComboBox*>(editor)->currentText(); 
     break; 
    } 
    case 4: 
    { 
     value = static_cast<QProgressBar*>(editor)->value(); 
     break; 
    } 
    } 

    model->setData(index, value); 
} 

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

모델.

#ifndef MODELACTIONS_H 
#define MODELACTIONS_H 

#include <QAbstractTableModel> 

#include <Unit.h> 

class ModelAction : public QAbstractTableModel 
{ 
    Q_OBJECT 
public: 
    explicit ModelAction(QObject *parent = 0); 
    int rowCount(const QModelIndex &parent = QModelIndex()) const; 
    int columnCount(const QModelIndex &parent = QModelIndex()) const; 
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; 
    Qt::ItemFlags flags(const QModelIndex &index) const; 
    QVariant headerData(int section, Qt::Orientation orientation, int role) const; 
    bool setData(const QModelIndex &index, const QVariant &value, int role); 
    void sort(int column, Qt::SortOrder order); 


    void setUnits(const QList<Unit*>* units); 

private: 
    const QList<Unit*>* units; 
    bool ascending[5]; 

}; 

#endif // MODELACTIONS_H 

#include "ModelAction.h" 

ModelAction::ModelAction(QObject *parent) : 
    QAbstractTableModel(parent), 
    units(0) 
{ 
} 

int ModelAction::rowCount(const QModelIndex &parent) const 
{ 
    if (units == 0) 
    { 
     return 0; 
    } 
    else 
    { 
     return units->length(); 
    } 
} 
int ModelAction::columnCount(const QModelIndex &parent) const 
{ 
    return 5; 
} 
QVariant ModelAction::data(const QModelIndex &index, int role) const 
{ 
    if (index.isValid() == false) 
    { 
     return QVariant(); 
    } 

    if (role == Qt::TextAlignmentRole) 
    { 
     if (index.column() == 0 || index.column() == 2) 
     { 
      return int(Qt::AlignLeft | Qt::AlignVCenter); 
     } 
     else 
     { 
      return int(Qt::AlignRight | Qt::AlignVCenter); 
     } 
    } 
    else if (role == Qt::DisplayRole) 
    { 
     if (index.column() == 0) 
     { 
      // Unit's id. 
      return index.row() + 1; 
     } 
     else if (index.column() == 1) 
     { 
      return "bob"; 
      // Unit's Action. 
      //return mechs.at(index.row())->getWeight(); 
     } 
     else if (index.column() == 2) 
     { 
      // Unit's Action start. 
      //return mechs.at(index.row())->getTechnology(); 
     } 
     else if (index.column() == 3) 
     { 
      // Unit's Action end. 
      //return Currency::numberToCurrency(mechs.at(index.row())->getPurchaseValue()); 
     } 
     else if (index.column() == 4) 
     { 
      // Unit's Action progress. 
      //return Currency::numberToCurrency(mechs.at(index.row())->getSellValue()); 
     } 
    } 
    return QVariant(); 
} 

QVariant ModelAction::headerData(int section, Qt::Orientation orientation, int role) const 
{ 
    if (role != Qt::DisplayRole) 
    { 
     return QVariant(); 
    } 

    if (orientation == Qt::Horizontal) 
    { 
     if (section == 0) 
     { 
      return "Id"; 
     } 
     else if (section == 1) 
     { 
      return "Action"; 
     } 
     else if (section == 2) 
     { 
      return "Begin Time"; 
     } 
     else if (section == 3) 
     { 
      return "End Time"; 
     } 
     else if (section == 4) 
     { 
      return "Progress"; 
     } 
    } 
    return QVariant(); 
} 

void ModelAction::sort(int column, Qt::SortOrder order) 
{ 
// MechCompare compare; 
// compare.column = column; 
// ascending[column] = !ascending[column]; 
// compare.ascending = ascending[column]; 
// qSort(mechs.begin(), mechs.end(), compare); 
    // reset(); 
} 

void ModelAction::setUnits(const QList<Unit *> *units) 
{ 
    this->units = units; 
    reset(); 
} 

Qt::ItemFlags ModelAction::flags(const QModelIndex &index) const 
{ 
    switch (index.column()) 
    { 
    case 0: 
    default: 
    { 
     return Qt::NoItemFlags; 
     break; 
    } 
    case 1: 
    { 
     return Qt::ItemIsEditable | Qt::ItemIsEnabled; 
    } 
    } 
} 

bool ModelAction::setData(const QModelIndex &index, const QVariant &value, int role) 
{ 
    switch (index.column()) 
    { 
    case 1: 
    { 

    } 
    } 
} 

내가 아는 유일한 문제는 ModelAction :: setData() 함수가 불완전하다는 것입니다. 이 서브 루틴을 완료하기 전에이 모델이 표시하는 데이터 클래스를 다시 편집해야합니다. 여전히 콤보 박스와 진행 막대가 계속 나타나야합니다. 아무 것도하지 마십시오.

이 시점에서 나는 ID 번호와 테이블의 각 행에 대한 테스트 텍스트 "bob"만 봅니다. QComboBox 및 QProgressBar는 이 아니며, 모두 렌더링됩니다..

도움이 될 것입니다.

JEC

답변

4

당신이 구현 된 위임 기능 편집자을위한 것입니다. 항목을 편집하지 않을 때는 표시되지 않습니다. 델리게이트 대신 QAbstractItemView::setIndexWidget을 원할 수도 있습니다.

+0

감사합니다. 오늘 나중에 확인해 보겠습니다. – jecjackal

+0

그래,이게 내가 찾고있는거야, 타이 – jecjackal

0

createEditor 메소드는 특정 이벤트 이후에만 호출됩니다. 예를 들어, 셀을 두 번 클릭 할 때 ... Buck이 지적한 것처럼 setIndexWidget을 사용해야합니다.