C++ (Qt)#include <QApplication>#include <QDateTime>#include <QAbstractItemModel>#include <QTreeView>#include <QDebug>//struct ItemInfo { QString m_SomeStuff; ItemInfo() : m_SomeStuff("") { }}; //class Node;typedef QList<Node*> QNodeList; class Node {public: Node *m_Parent; int m_Row; QNodeList m_Children; QString m_Name; Node(Node *parent, int row, QString name): m_Parent(parent), m_Row(row), m_Name(name) { } void traverse(int level = 0) { qDebug() << QString().fill(' ', level) << m_Name; foreach (Node *child, m_Children) child->traverse(level + 1); }}; class DirNode: public Node {public: DirNode(Node *parent, int row, QString name): Node(parent, row, name) { }}; class FileNode: public Node {public: ItemInfo m_Info; FileNode(Node *parent, int row, QString name, ItemInfo info): Node(parent, row, name), m_Info(info) { }};//class IndexModel: public QAbstractItemModel {private: Node *m_RootNode; inline Node* indexData(const QModelIndex &index) const { return static_cast<Node*>(index.internalPointer()); } public: IndexModel(QObject *parent): QAbstractItemModel(parent) { m_RootNode = new Node(0, -1, ""); Node *baseRootNode = m_RootNode; m_RootNode->m_Children.append(new FileNode(m_RootNode, 0, "Bar", ItemInfo())); m_RootNode->m_Children.append(new DirNode(m_RootNode, 1, "Foo")); m_RootNode = m_RootNode->m_Children.last(); m_RootNode->m_Children.append(new FileNode(m_RootNode, 0, "FooBar", ItemInfo())); m_RootNode->m_Children.append(new DirNode(m_RootNode, 1, "Baz")); m_RootNode = m_RootNode->m_Children.last(); m_RootNode->m_Children.append(new FileNode(m_RootNode, 0, "FooBarBaz", ItemInfo())); m_RootNode->m_Children.append(new DirNode(m_RootNode, 1, "Bazzz")); m_RootNode = baseRootNode; m_RootNode->traverse(); } QModelIndex index(int row, int column, const QModelIndex &parent) const { if (!parent.isValid()) return createIndex(row, column, m_RootNode->m_Children.at(row)); else return createIndex(row, column, indexData(parent)->m_Children.at(row)); } QModelIndex parent(const QModelIndex &index) const { if (!indexData(index)->m_Parent) return QModelIndex(); else return createIndex(indexData(index)->m_Row, index.column(), indexData(index)->m_Parent); } int columnCount(const QModelIndex &parent) const { Q_UNUSED(parent) return 4; } int rowCount(const QModelIndex &parent) const { if (!parent.isValid()) return m_RootNode->m_Children.count(); else return indexData(parent)->m_Children.count(); } QVariant data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) return indexData(index)->m_Name; else return QVariant(); } QVariant headerData(int section, Qt::Orientation orientation, int role) const { Q_UNUSED(section) if (orientation == Qt::Horizontal && role == Qt::DisplayRole) return ""; else return QVariant(); }};//class IndexViewer: public QTreeView {public: IndexViewer() { setModel(new IndexModel(this)); for (int i = 0; i < model()->columnCount(QModelIndex()); ++i) resizeColumnToContents(i); }};//int main(int argc, char *argv[]) { QApplication app(argc, argv); IndexViewer iv; iv.show(); return app.exec();}
QModelIndex parent(const QModelIndex &index) const { if (!indexData(index)->m_Parent) return QModelIndex(); else return createIndex(indexData(index)->m_Row, index.column(), indexData(index)->m_Parent); }
Conditional jump or move depends on uninitialised value(s)at 0x804D94A: QList<Node*>::at(int) const (qlist.h:395)by 0x804D37C: IndexModel::index(int, int, QModelIndex const&) const (main.cpp:86)by 0x46DC7D6: QTreeView::drawRow(QPainter*, QStyleOptionViewItem const&, QModelIndex const&) const (in /usr/lib/libQtGui.so.4.5.2)by 0x46E4C3B: QTreeView::drawTree(QPainter*, QRegion const&) const (in /usr/lib/libQtGui.so.4.5.2)by 0x46E56BA: QTreeView::paintEvent(QPaintEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x41B0A13: QWidget::event(QEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x45570E2: QFrame::event(QEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x45F5873: QAbstractScrollArea::viewportEvent(QEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x46A8D76: QAbstractItemView::viewportEvent(QEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x46E3DE3: QTreeView::viewportEvent(QEvent*) (in /usr/lib/libQtGui.so.4.5.2)by 0x45F7E64: ??? (in /usr/lib/libQtGui.so.4.5.2)by 0x4D6A9F9: QCoreApplicationPrivate::sendThroughObjectEventFilters(QObject*, QEvent*) (in /usr/lib/libQtCore.so.4.5.2)
C++ (Qt) QModelIndex parent(const QModelIndex &index) const { if (!index.isValid()) return QModelIndex(); Node* node = static_cast<Node*>(index.internalPointer()); Node* parent = node->m_Parent; if(parent == m_RootNode) return QModelIndex(); return createIndex(indexData(index)->m_Row, index.column(), indexData(index)->m_Parent); }
QVariant data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) return indexData(index)->m_Name; else return QVariant(); }
QVariant data(const QModelIndex &index, int role) const { return QVariant(); }