謎:Qtでは、editorEventが呼び出されるのにcreateEditorは呼び出されないのはなぜですか?

StackOverflow https://stackoverflow.com/questions/828819

  •  06-07-2019
  •  | 
  •  

質問

QAbstractItemDelegateをサブクラス化しています。これは私のコードです。提案は大歓迎です:

QWidget *ParmDelegate::createWidget(Parm *p, const QModelIndex &index) const {
    QWidget *w;
    if (index.column() == 0) {
        w = new QLabel(p->getName().c_str());
    } else {
        if (p->isSection())
            return NULL;
        w = p->createControl();
    }
    return w;
}

QWidget *ParmDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    cout << "createEditor called" << endl;
    Parm    *p = reinterpret_cast<Parm*>(index.internalPointer());
    QWidget *retval = createWidget(p, index);
    retval->setFocusPolicy(Qt::StrongFocus);
    retval->setParent(parent);
    return retval;
}

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

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

QSize ParmDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
    Parm    *p = reinterpret_cast<Parm*>(index.internalPointer());
    scoped_ptr<QWidget> w(createWidget(p, index));
    if (!w)
        return QSize(0,0);
    return w->sizeHint();
}

bool ParmDelegate::editorEvent(QEvent * event, QAbstractItemModel * model, const QStyleOptionViewItem & option, const QModelIndex & index ) {
    cout << "editorEvent called" << endl;
    return false;
}

これを実行すると、editEventごとにeditorEventが編集イベントごとに2回呼び出されるだけです!

役に立ちましたか?

解決

Qtの AbstractItemDelegate ドキュメントから:

  

カスタム編集を提供するには、2つのアプローチを使用できます。最初のアプローチは、エディターウィジェットを作成し、アイテムの上に直接表示することです。これを行うには、createWidget()を再実装してエディターウィジェットを提供し、setEditorData()を使用してエディターからモデルのデータを取り込み、setModelData()を使用して、デリゲートがエディターのデータでモデルを更新できるようにする必要があります。

     

2番目のアプローチは、editorEvent()を再実装することにより、ユーザーイベントを直接処理することです。

これは、最初のアプローチをトリガーするものが不足していることを示しているようです。私の推測では、モデルの data()関数は Qt :: EditRole オプションに適切な値を返していません。

他のヒント

QItemDelegateから継承したTableViewを実装しました。その後、私は同様の問題がありました。 「return QItemDelegate :: editorEvent(event、model、option、index);」を呼び出さないように追跡しましたeditorEvent(...)メソッドで。

これを試すことができます。多分それが役立ちます。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top