[SOLVED] Qt TreeView does not show Tree



  • Hi

    i cant get why my DataContainer or the Model does not work with the TreeView. It's nearly the same as the editabletreemodel example. There are nor errors it doesn't show the Data or the Header.

    The DataContainer contains the Data like i would expect. So it is most likely the model!

    Any ideas?

    @#include "reqmodel.h"

    #include <iostream>

    ReqModel::ReqModel(ReqItem* root,QObject *parent)
    :QAbstractItemModel(parent)
    {
    std::cout<<"inst"<<std::endl;
    rootItem=root;
    }

    ReqModel::~ReqModel()
    {
    delete rootItem;
    }

    QVariant ReqModel::data(const QModelIndex &index, int role) const
    {
    std::cout<<"data"<<std::endl;
    if (!index.isValid())
    return QVariant();

    if (role != Qt::DisplayRole && role != Qt::EditRole)
        return QVariant();
    
    
    ReqItem *item = getItem(index);
    
    return item->data(index.column());
    

    }

    QVariant ReqModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
    std::cout<<"headerdata"<<std::endl;

    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return rootItem->data(section);
    
    return QVariant();
    

    }

    QModelIndex ReqModel::index(int row, int column, const QModelIndex &parent) const
    {
    std::cout<<"index"<<std::endl;
    if (parent.isValid() && parent.column() != 0)
    return QModelIndex();

    ReqItem *parentItem = getItem(parent);
    
    ReqItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
    

    }

    QModelIndex ReqModel::parent(const QModelIndex &index) const
    {
    std::cout<<"parent"<<std::endl;
    if (!index.isValid())
    return QModelIndex();

    ReqItem *childItem = getItem(index);
    ReqItem *parentItem = childItem->parent();
    
    if (parentItem == rootItem)
        return QModelIndex();
    
    return createIndex(parentItem->childNumber(), 0, parentItem);
    

    }

    int ReqModel::rowCount(const QModelIndex &parent) const
    {
    std::cout<<"rowcount"<<std::endl;
    ReqItem *parentItem = getItem(parent);

    return parentItem->childCount();
    

    }

    int ReqModel::columnCount(const QModelIndex &parent) const
    {
    Q_UNUSED(parent);
    return rootItem->columnCount();
    }

    Qt::ItemFlags ReqModel::flags(const QModelIndex &index) const
    {
    if (!index.isValid())
    return 0;

    return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
    

    }

    bool ReqModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    std::cout<<"setdata"<<std::endl;
    if (role != Qt::EditRole)
    return false;

    ReqItem *item = getItem(index);
    bool result = item->setData(index.column(), value);
    
    if (result)
        emit dataChanged(index, index);
    
    return result;
    

    }

    bool ReqModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
    {
    std::cout<<"setheader"<<std::endl;
    if (role != Qt::EditRole || orientation != Qt::Horizontal)
    return false;

    bool result = rootItem->setData(section, value);
    
    if (result)
        emit headerDataChanged(orientation, section, section);
    
    return result;
    

    }

    bool ReqModel::insertColumns(int position, int columns, const QModelIndex &parent)
    {
    bool success;

    beginInsertColumns(parent, position, position + columns - 1);
    success = rootItem->insertColumns(position, columns);
    endInsertColumns();
    
    return success;
    

    }

    bool ReqModel::removeColumns(int position, int columns, const QModelIndex &parent)
    {
    bool success;

    beginRemoveColumns(parent, position, position + columns - 1);
    success = rootItem->removeColumns(position, columns);
    endRemoveColumns();
    
    if (rootItem->columnCount() == 0)
        removeRows(0, rowCount());
    
    return success;
    

    }

    bool ReqModel::insertRows(int position, int rows, const QModelIndex &parent)
    {
    ReqItem *parentItem = getItem(parent);
    bool success;

    beginInsertRows(parent, position, position + rows - 1);
    success = parentItem->insertChildren(position, rows, rootItem->columnCount());
    endInsertRows();
    
    return success;
    

    }

    bool ReqModel::removeRows(int position, int rows, const QModelIndex &parent)
    {
    ReqItem *parentItem = getItem(parent);
    bool success = true;

    beginRemoveRows(parent, position, position + rows - 1);
    success = parentItem->removeChildren(position, rows);
    endRemoveRows();
    
    return success;
    

    }

    ReqItem *ReqModel::getItem(const QModelIndex &index) const
    {
    std::cout<<"getitem"<<std::endl;
    if (index.isValid()) {
    ReqItem item = static_cast<ReqItem>(index.internalPointer());
    if (item)
    return item;
    }
    return rootItem;
    }

    @



  • Do your RowCount and ColumnCount return the right numbers??



  • Thx it was the column count!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.