找不到有关文本编辑项目委托定义的错误

Can't find error on definition of TextEdit Item Delegate

本文关键字:定义 错误 项目 文本编辑 找不到      更新时间:2023-10-16

那么,出现的错误如下

<>之前… build-ChequesV2-Desktop_Qt_5_2_1_MinGW_32bit-Debug texteditdelegate调试。0:-1:在函数ZN16TextEditDelegateC2EP7QObject中:ChequesV2 TextEditDelegate .cpp:8:错误:未定义引用"textteditdelegate的虚表"Collect2.exe:-1: error: error: ld返回1退出状态之前

我读了很多次代码,我找不到错误在哪里,我把它和其他2个委托的代码比较了一下,似乎没有问题。

标题:

#ifndef TEXTEDITDELEGATE_H
#define TEXTEDITDELEGATE_H
#include <QStyledItemDelegate>
class TextEditDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    TextEditDelegate(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;
};
#endif // TEXTEDITDELEGATE_H

和实现:

#include "texteditdelegate.h"
#include <QStyledItemDelegate>
#include <QInputDialog>

TextEditDelegate::TextEditDelegate(QObject *parent): QStyledItemDelegate(parent)
{
}
QWidget *TextEditDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QInputDialog *editor = new QInputDialog(parent);
    editor->setOption(QInputDialog::UsePlainTextEditForTextInput);
    editor->setInputMode(QInputDialog::TextInput);
    editor->setLabelText("Ingrese el concepto del cheque");
    return editor;
}

void TextEditDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) const
{
    QString value = index.model()->data(index, Qt::EditRole).toString();
    QInputDialog *inputDialog = static_cast<QInputDialog*>(editor);
    inputDialog->setTextValue(value);
}

void TextEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) const
{
    QInputDialog *inputDialog = static_cast<QInputDialog*>(editor);
    if (!inputDialog) return;
    model->setData(index, inputDialog->textValue()/*, Qt::EditRole*/);
}

下面是调用:

view = new QTableView;
view->setModel(tableProxy);
view->setItemDelegateForColumn(COLUMNADECONCEPTO, new TextEditDelegate(view));

虽然注释最后一行没有改变什么,但错误仍然显示。

您似乎是静态强制转换的QWidget指针。那不是个好主意。

QInputDialog *inputDialog = static_cast<QInputDialog*>(editor);

简而言之,static_cast用于在编译时确实知道从一种类型转换为另一种类型是安全的情况。

然而,在这种情况下,它是一个运行时决策,无法在编译期间按照您的期望进行评估。在这种情况下,您需要在c++中使用dynamic_cast,但在Qt世界中,qobject_cast在处理QObjects时甚至更好。

+ textdelegate (QObject *parent = 0);应该改成这样:TextEditDelegate(QWidget *parent = 0);

这可能是转移注意力,错误的结论。