C ++ Qt:虽然调用了paint(),但从未调用过QEtyledItemDelegate的createEditor

时间:2017-06-02 16:53:22

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

我有一个 var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; var range = sheet.getRange("A1:C7"); // Sorts by the values in the first column (A) range.sort(1); // Sorts by the values in the second column (B) range.sort(2); // Sorts descending by column B range.sort({column: 2, ascending: false}); // Sorts descending by column B, then ascending by column A // Note the use of an array range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]); // For rows that are sorted in ascending order, the "ascending" parameter is // optional, and just an integer with the column can be used instead. Note that // in general, keeping the sort specification consistent results in more readable // code. We could have expressed the earlier sort as: range.sort([{column: 2, ascending: false}, 1]); // Alternatively, if we wanted all columns to be in ascending order, we would use // the following (this would make column 2 ascending) range.sort([2, 1]); // ... which is equivalent to range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]); ,我想显示一个带有进度条和其他一些字段的简单小部件(可能还有一些上下文菜单,但目前我只想显示小部件)。该列表下面有一个模型,模型成功地将一个字符串传递给列表,没有委托,一切正常。

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

我在sizeHint()上看到的基本上是简单的文字项目。小部件永远不会显示(当然,因为永远不会调用QListView)。

以下是我的代表......非常基本!

createEditor()

和来源(也非常基本和最小):

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(); } )显示在列表中。

1 个答案:

答案 0 :(得分:3)

您尚未实施FilesQueue::flags(const QModelIndex &index) const。 默认实现使单元格仅启用并可选择。

请参阅documentation