Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Adding text to same row, but next column?
Forum Updated to NodeBB v4.3 + New Features

Adding text to same row, but next column?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SRaD
    wrote on last edited by
    #1

    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.

    VRoninV 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Are y looking for QTreeWidgetItem::child()?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SRaD
        wrote on last edited by
        #3

        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?

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then you should take a look at QTreeWidgetItem::QTreeWidgetItem(QTreeWidget *parent, const QStringList &strings, int type = Type)

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • S SRaD

            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?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @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.

            1 Reply Last reply
            1
            • S SRaD

              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.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @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);

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              3
              • S Offline
                S Offline
                SRaD
                wrote on last edited by
                #7

                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?

                JonBJ 1 Reply Last reply
                0
                • S SRaD

                  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?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @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?

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    S 1 Reply Last reply
                    2
                    • Christian EhrlicherC Christian Ehrlicher

                      @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?

                      S Offline
                      S Offline
                      SRaD
                      wrote on last edited by
                      #10

                      @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.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        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"

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        1

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved