找不到有关文本编辑项目委托定义的错误
Can't find error on definition of TextEdit Item Delegate
那么,出现的错误如下
<>之前… 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);
这可能是转移注意力,错误的结论。
相关文章:
- 使用命名空间时出现多个定义错误
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在头文件和 cpp 文件中使用一次 #pragma 时出现结构重定义错误
- 链接阶段出现多重定义错误
- 避免模板类中的重定义错误
- 即使我没有包含多个文件,C++中的多个定义错误
- 跨多个类的全局变量而不会出现重定义错误?
- 尝试运行 wasm 函数时出现模块未定义错误
- C++ 预期的左大括号以及重定义错误
- C++:成员的越界声明必须是纯虚函数的定义错误
- c++中数组的未定义错误
- Visual C:模板类中的自定义错误消息
- 为什么C++模板不会导致多个定义错误?
- 只有一个定义/声明时标头声明变量的多堆定义错误
- C++ 在多个其他类中使用单个类 - 编译时出现多个定义错误
- 基类未定义.错误 C2504
- Q 斯坦达项重定义错误
- 可视化C++中的结构定义错误
- VC++ C2011 重定义错误 - 未使用的头文件