[Qt-interest] Question about Editable Tree Model Example

Damian Hischier himbow at gmail.com
Fri Dec 11 09:22:25 CET 2009


Hello

I played around with the examples/itemviews/editabletreemodel because
I would like to use something similar in one of my applications.

If I select an item by clicking on the first column (Title) and click
on Actions->Insert Child everything works fine. A new child empty item
will be inserted. But if I select an item by clicking on the second
column (Description), then something strange happens. I figured out
the something goes wrong in this function:

examples/itemviews/editabletreemodel/mainwindow.cpp:

void MainWindow::insertChild()
{
    QModelIndex index = view->selectionModel()->currentIndex();
    QAbstractItemModel *model = view->model();

    if (model->columnCount(index) == 0) {
        if (!model->insertColumn(0, index))
            return;
    }

    if (!model->insertRow(0, index))
        return;

    for (int column = 0; column < model->columnCount(index); ++column) {
        QModelIndex child = model->index(0, column, index);
        model->setData(child, QVariant("[No data]"), Qt::EditRole);
        if (!model->headerData(column, Qt::Horizontal).isValid())
            model->setHeaderData(column, Qt::Horizontal, QVariant("[No
header]"),
                                 Qt::EditRole);
    }

    view->selectionModel()->setCurrentIndex(model->index(0, 0, index),

QItemSelectionModel::ClearAndSelect);
    updateActions();
}

For some reason child.row() and child.column() is -1. But this only
happens if I click on the second column (Description) in order to
select the item in the treeView.

Does anybody have an idea what is going wrong there and how I can fix that?

Thank you very much for your help.

Damian



More information about the Qt-interest-old mailing list