Qt World Summit: Submit your Presentation

How to add text to second column along with first?

  • Using Qt5, I have a TreeModel which inherits QAbstractItemModel. I have two colums in the view. I can dynamically add a checkbox with a label to the first column using the following:

    QCheckBox *cb = new QCheckBox;
    QVector<QVariant> myData;
    myData << name << QVariant::fromValue(cb);
    MyTreeItem *myItem = new MyTreeItem(myData, mainItem);

    What I'd like to do now is add a text string to the second column on the same row where I added this MyTreeItem.

    How might that be done?

  • @srad said in How to add text to second column along with first?:

    new MyTreeItem

    In examples, method data for TreeModel has form:

    QVariant TreeModel::data(const QModelIndex &index, int role) const
        if (!index.isValid())
            return QVariant();
        if (role != Qt::DisplayRole)
            return QVariant();
        TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
        return item->data(index.column());

    And for TreeItem method data:

    QVariant TreeItem::data(int column) const
        return m_itemData.value(column);

    Your case depends on your implementation that methods and constructor of MyTreeItem.

Log in to reply