QML TableView没有调用QAbstractTableModel headerData() - 函数

时间:2014-09-22 15:27:54

标签: c++ qt qml qtquick2

我目前正在使用QML / QtQuick TableView。数据显示 - &gt;大。我目前唯一没有开始工作的是标题:据我了解Qt模型概念,TableView应该自动调用C ++ QAbstractTableModel::headerData(int section, Qt::Orientation orientation, int role) - 函数。但是,当我在函数的开头添加qDebug() << "Hello World!"(或使用调试器;))时,永远不会调用此函数,并且TableView的标题不会显示任何文本。

有人有这个工作吗?或者由于某种原因(Qt 5.3)没有实现(但是?)?替代品会是什么?

编辑:添加

mymodel.cpp

QVariant myModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    qDebug() << "Hello World!";
}

[... I left out the implementation of the other functions
 here as they work as expected ...]

mymodel.h

#ifndef MYMODEL_H
#define MYMODEL_H

#include <QAbstractTableModel>

#include "inputvectors.h"

class MyModel : public QAbstractTableModel
{
    Q_OBJECT

public:
    MyModel (QObject *parent = 0);
    // InputVectors is just another class I use but it basically
    // contains two `QList`s
    MyModel (InputVectors *inputData, QObject *parent=0);

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

protected:
    QHash<int, QByteArray> roleNames() const;

private:
    InputVectors *inputData;
};

#endif // MYMODEL_H

main.qml

[...]
TableView {
    id: myTableView
    anchors.fill: parent
    frameVisible: false
    model: controller.getInputVectorsModel()

    TableViewColumn {
        role: "row_line_numbers"
        width: 40
        delegate: Item {
            Text { text: styleData.value; anchors.left:parent.left;anchors.leftMargin: 12}
        }
    }
}
[...]

0 个答案:

没有答案