Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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;
    cb->setCheckState(Qt::Checked);
                                                                               
    QVector<QVariant> myData;
    myData << name << QVariant::fromValue(cb);
                                                                               
    MyTreeItem *myItem = new MyTreeItem(myData, mainItem);
    myItem->set_label(sname);
    
    mainItem->appendChild(myItem);
    

    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