gpt4 book ai didi

c++ - 在 TextEdit Item Delegate 的定义中找不到错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:07 25 4
gpt4 key购买 nike

嗯,出现的错误如下

...\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 status

I have read the code a lot of times, and I can't find where the error is, I have compared it with the codes of other 2 delegates and it seems to be all right.

here is the header:

#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 时甚至更好。

Plus TextEditDelegate(QObject *parent = 0); Should change to this: TextEditDelegate(QWidget *parent = 0);

这可能是转移注意力的错误结论。它也应该与 QObject 一起工作。

关于c++ - 在 TextEdit Item Delegate 的定义中找不到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com