Displaying Qt tree through QAbstractItemModel and QModelIndex

104 views Asked by At

I'm trying to display a user defined tree Family_tree through a TreeModel class derived from QAbstractItemModel containing a Family_tree ft_ member and the overrides for the index, parent, data etc methods. I rewrote the methods using the Family_tree nodes and I'm calling them from MainWindow like shown below (I double checked Family_tree and it is being set up correctly). All I see is empty space with a single '1' appearing on the top left corner since I have columnCount return 1 for all the nodes and I'm starting by trying to display a tree with only one node (I only expect icons to show up).

Am I misinterpreting how this works in any way? I would really appreciate some insight, thank you for your time!

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    inh_pattern = AD;
    patient = new Patient("Nancy Kalaj", Date(12, 12, 1997), 25, 'F', Genotype("DD"), Date());
    family = new Family_tree(*patient);
    selected = new SelectedPatient(); selected->setSelectedPatient(patient);

    patientOverview = new PatientOverview(*patient, *selected, this); patientOverview->show();
    patientRelatives = new PatientRelatives(*family, *selected, this);
    treeModel = new TreeModel(*family);
    QTreeView* view = new QTreeView(this);
    view->setModel(treeModel);
    //...
}

QModelIndex TreeModel::index(int row, int column, const QModelIndex& parent) const {
    if (!hasIndex(row, column, parent)) {return QModelIndex();}

    node* parentNode;
    if (!parent.isValid()) {parentNode = ft_.get_root();}
    else {parentNode = static_cast<node*>(parent.internalPointer());}

    node* childNode = parentNode->getChildren()[row];
    if (childNode) {return createIndex(row, column, childNode);}
    else {return QModelIndex();}
}

QModelIndex TreeModel::parent(const QModelIndex& index) const {
    if (!index.isValid()) {return QModelIndex();}

    node* n = static_cast<node*>(index.internalPointer());
    node* parentNode = n->getParent();
    if (parentNode == ft_.get_root()) {return QModelIndex();}

    return createIndex(row(parentNode), 0, parentNode);
}

bool TreeModel::hasChildren(const QModelIndex& parent) const {
    if (!parent.isValid()) {
           return true;
       }
       node* n = static_cast<node*>(parent.internalPointer());
       return !n->getChildren().empty();
}

int TreeModel::rowCount(const QModelIndex& parent) const {
    node* parentNode;
    if (parent.column() > 0) {return 0;}

    if (!parent.isValid()) {parentNode = ft_.get_root();}
    else {parentNode = static_cast<node*>(parent.internalPointer());}

    return parentNode->getChildren().size();
}


int TreeModel::columnCount(const QModelIndex& parent) const {
    return 1;
}

QVariant TreeModel::data(const QModelIndex &index, int role) const {
    if (!index.isValid()) {return QVariant();}

    node* n = static_cast<node*>(index.internalPointer());
    if (role == Qt::DecorationRole) {
        if (index.column() == 0) {
            QPixmap pixmap(16, 16);
            pixmap.fill(Qt::black);
            QPainter painter(&pixmap);
            painter.setBrush(Qt::white);
            painter.drawRect(0, 0, 15, 15);
            return QIcon(pixmap);
        }
        else {return QVariant();}
    }
    return QVariant();
}

Qt::ItemFlags TreeModel::flags(const QModelIndex& index) const {
    if (!index.isValid()) {return Qt::NoItemFlags;}
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
0

There are 0 answers