C++ Can';在TextEdit项委托的定义上找不到错误
嗯,出现的错误如下 ...\build-ChequesV2-Desktop_Qt_5_2_1_MinGW_32bit-Debug\debug\texteditdelegate.o:-1: In function `ZN16TextEditDelegateC2EP7QObject': ChequesV2\texteditdelegate.cpp:8: error: undefined reference to `vtable for TextEditDelegate' collect2.exe:-1: error: error: ld returned 1 exit statusC++ Can';在TextEdit项委托的定义上找不到错误,c++,qt,qtableview,qtgui,qdialog,C++,Qt,Qtableview,Qtgui,Qdialog,嗯,出现的错误如下 ...\build-ChequesV2-Desktop_Qt_5_2_1_MinGW_32bit-Debug\debug\texteditdelegate.o:-1: In function `ZN16TextEditDelegateC2EP7QObject': ChequesV2\texteditdelegate.cpp:8: error: undefined reference to `vtable for TextEditDelegate' collect2.exe:
尽管注释最后一行不会改变任何内容,但仍然会显示错误。您似乎在静态地使用
QWidget
指针。这不是一个好主意
QInputDialog *inputDialog = static_cast<QInputDialog*>(editor);
QInputDialog*inputDialog=static\u cast(编辑器);
简而言之,static_cast用于在编译时知道从一种类型转换到另一种类型是安全的情况
但是,在本例中,这是一个运行时决策,在编译期间无法按预期进行评估。在C++中,你需要使用DyrimeSkyCube,但是在Qt世界中,QObjutsCube在处理QObjts时甚至更好。
加上TextEditDelegate(QObject*parent=0);应更改为:TextEditDelegate(QWidget*parent=0)
这可能是在转移视线,而且是错误的结论。它也应该适用于QObject。不要在问题中添加“已解决”。如果您的答案与已经发布的答案不同,欢迎您回答自己的问题。请编辑您的问题,使其不包含答案。虽然我在下面的人之前回答了我的问题,但我编辑了我的问题,使其不包含答案。@user2348235:“该人”正在询问您是否有机会继承QWidget。;)顺便说一句,你要求解释,所以我给了。希望,这有帮助,但正如库巴所说,你可以自由发布自己的解决方案。尽管如此,当你在问题中没有提供足够的信息时,它看起来很模糊。拉兹洛感谢你的解释。
#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));
QInputDialog *inputDialog = static_cast<QInputDialog*>(editor);