C++ Qt : QStyledItemDelegate's createEditor is never called, 尽管调用了 paint()

标签 c++ qt model-view-controller qt5 qitemdelegate

我有一个QListView,我想在其中显示一个带有进度条和一些其他字段的简单小部件(也许还有一些上下文菜单,但目前我只想显示小部件) .该列表下面有一个模型,该模型成功地将一个字符串传递给列表,并且在没有委托(delegate)的情况下一切正常。

现在有了委托(delegate),createEditor() 方法从不被调用。我不明白为什么。我不需要绘画,但我只是覆盖 paint()sizeHint() 以查看它们是否被调用,它们是否被调用。

我在QListView 上看到的基本上是简单的文本项。小部件永远不会出现(当然,因为永远不会调用 createEditor())。

以下是我的委托(delegate)...非常基本!

class FileQueueItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    FileQueueItemDelegate(QObject *parent = 0);
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
    void setEditorData(QWidget *editor, const QModelIndex &index) const override;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
    void updateEditorGeometry(QWidget *editor,
                              const QStyleOptionViewItem &option,
                              const QModelIndex &index) const override;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QSize sizeHint(const QStyleOptionViewItem &option,
                   const QModelIndex &index) const override;
};

和源代码(也是非常基本和最小的):

#include "FileQueueItemDelegate.h"

FileQueueItemDelegate::FileQueueItemDelegate(QObject *parent) : QStyledItemDelegate(parent)
{

}

QWidget *FileQueueItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    std::cout<<"Creating editor..."<<std::endl;
    FileQueueListItem* item = new FileQueueListItem(parent);
    return item;
}

void FileQueueItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    std::cout<<"Setting editor data..."<<std::endl;
    FileQueueListItem* editorPtr = dynamic_cast<FileQueueListItem*>(editor);
    QVariant dataResult = index.model()->data(index, Qt::DisplayRole);
    editorPtr->setFilename(dataResult.toString());
}

void FileQueueItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    std::cout<<"Setting model data..."<<std::endl;
}

void FileQueueItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    std::cout<<"Updating editor geometry..."<<std::endl;
    editor->setGeometry(option.rect);
}

void FileQueueItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    std::cout<<"Painting..."<<std::endl;
    QStyledItemDelegate::paint(painter, option, index);
}

QSize FileQueueItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    std::cout<<"Size hint..."<<std::endl;
    return QStyledItemDelegate::sizeHint(option, index);
}

我可以看到 paint()sizeHint() 的打印,但看不到其他(永远)。

下面是我的QListView类的构造函数(我继承自它):

FilesQueueQList::FilesQueueQList(FilesQueue* queueObjectPtr)
{
    this->internal_queue = queueObjectPtr;
    this->setModel(internal_queue); 
    itemDelegate = new FileQueueItemDelegate(this);
    this->setItemDelegate(itemDelegate);
}

下面是它的定义:

class FilesQueueQList : public QListView
{
    Q_OBJECT
    FilesQueue* internal_queue; //this inherits from QAbstractListModel
    FileQueueItemDelegate* itemDelegate;

//...
}

最后,这是模型中的 data() 方法:

QVariant FilesQueue::data(const QModelIndex &index, int role) const
{
    std::cout<<"Loading data of "<<index.row()<< " "<<index.column()<<std::endl;
    if ( role == Qt::DisplayRole ) {
        return filesQueue[index.row()]->getFilename();
    }
    if(role == Qt::EditRole) {
        return filesQueue[index.row()]->getFilename(); //QString this is
    }
    return QVariant();
}

请协助。如果您需要更多信息,请告诉我。我一直在尝试一整天,但没有成功。我只想让该小部件 (FileQueueListItem) 显示在列表中。

最佳答案

您还没有实现 FilesQueue::flags(const QModelIndex &index) const。 默认实现使单元格仅启用和可选择。

参见文档:QAbstractItemModel::flags

Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex &index) const

Returns the item flags for the given index.

The base class implementation returns a combination of flags that enables the item (ItemIsEnabled) and allows it to be selected (ItemIsSelectable).

关于C++ Qt : QStyledItemDelegate's createEditor is never called, 尽管调用了 paint(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44333915/

相关文章:

c++ - 将文本编辑的滚动条移至顶部

c++ - 用Qt打印CGAL排列

javascript - 使用 agility.js 进行页面布局和组合

c# - MVC C# web 服务连接两个表并合并输出

c++ - C语言上 "static const char array"可以包含变量的成员吗

c++ - 传递整数文字的更快方法?

c++ - 对于已定义的符号,静态库链接问题 "Undefined symbols"

c++ - 操作系统实际上做了什么?

用于 Mac 上 Qt 的 MySQL

model-view-controller - 我可以在 asp.net core 2.0.0 中使用 razor base web 应用程序进行授权吗?