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

Adding text to same row, but next column?



  • Using Qt5, I have MyTreeModel 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 *box = new QCheckBox;
    box->setCheckState(Qt::Checked);
                                                                               
    QVector<QVariant> myData;
    myData << name << QVariant::fromValue(box);
                                                                               
    MyTreeItem *myItem = new MyTreeItem(myData, mainItem);
    
    mainItem->appendChild(myItem);
                                                                               
    emit treeModel->layoutChanged();
    

    Each time I append items to mainItem, this results in a hierarchy that all happens in column 0 of the tree. What I'd like to do now is add a text string to the second column (column 1) on the same row where I added "myItem" in column 0.

    I'm just not sure how to add text to a second colum whereas, in the above, I just did an appendChild on a current item. I would like to be able to add it right after the code above at the same time.


  • Lifetime Qt Champion

    Are y looking for QTreeWidgetItem::child()?



  • I don't believe so. When I create the TreeView, there are two column headers. When I append a child node to the parent underneath the first column (first header), my understanding is that this is two "sub-like" columns underneath the first header, or column 0. I'm trying to add this underneath the second column header, yet on the same row of the child I just appended. Does that make sense?


  • Lifetime Qt Champion



  • @SRaD
    As @Christian-Ehrlicher has written, the constructor overload he shows allows you to set a QTreeWidgetItem's column strings from the start. In case you need it, QTreeWidgetItem::setText(int column, const QString &text) & QTreeWidgetItem::setData(int column, int role, const QVariant &value) allow you to access individual columns' text/data at will.



  • @SRaD said in Adding text to same row, but next column?:

    I can dynamically add a checkbox with a label to the first column using the following:
    QCheckBox *box = new QCheckBox;

    No! I know what you are doing, I can feel the evil inside MyTreeItem. The correct way to do this is to set a Qt::CheckState inside the Qt::CheckStateRole of the item data.

    @SRaD said in Adding text to same row, but next column?:

    What I'd like to do now is add a text string to the second column (column 1) on the same row where I added "myItem" in column 0

    As with all models: model->setData(model->index(row,column,parent),QStringLiteral("MyText"),Qt::EditRole);



  • After looking QTreeWidgetItem over, I'm not sure this is what I need.

    I have a custom model which is derived from QAbstractItemModel.

    When looking at the links above to QTreeWidgetItem, in it's description is "The given list of strings will be set as the item text for each column in the item." I'm not doing it like this. See first post for how an item is added.

    Now I'm just trying to add a text string to the next colum over on a select few number of rows. Am I off base here?



  • @SRaD
    Not sure what you are still asking? As both I & @VRonin have said, you can add individual columns to individual rows dynamically after construction via either QTreeWidgetItem::setText(int column, const QString &text) or QTreeWidgetItem::setData(int column, int role, const QVariant &value), or QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) where index refers to a subsequent column if you're rolling your own. Is that not what you are asking?


  • Lifetime Qt Champion

    @SRaD said in Adding text to same row, but next column?:

    I have a custom model which is derived from QAbstractItemModel.

    Your code in your initial post did not say anything about this so we assumed you're using a custom QStandardItem (due to the name MyTreeItem).
    So since you don't use QStandardItemModel - can you show us your model implementation? How do you add the items to your model at all?



  • @Christian-Ehrlicher said in Adding text to same row, but next column?:

    Your code in your initial post did not say anything about this so we assumed you're using a custom QStandardItem (due to the name MyTreeItem).
    So since you don't use QStandardItemModel - can you show us your model implementation? How do you add the items to your model at all?

    But my OP say "which inherits QAbstractItemModel." and it does have code to show how I add items to the model. Doesn't deriving from QAbstractItemModel mean I am making a custom model? This confuses me.

    My code is basically the same as
    https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html#

    Where each item is just a plain class that I use with a class derived from QAbstractItemModel. Isn't this a custom model.


  • Lifetime Qt Champion

    So when you take a look at this model you will see

    QVariant TreeItem::data(int column) const
    {
        if (column < 0 || column >= m_itemData.size())
            return QVariant();
        return m_itemData.at(column);
    }
    

    And here is the full description for this class: https://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html#treeitem-class-definition
    "Information about the number of columns associated with the item is provided by columnCount(), and the data in each column can be obtained with the data() function"


Log in to reply