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. How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.

How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.

Scheduled Pinned Locked Moved Unsolved General and Desktop
45 Posts 3 Posters 5.9k 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.
  • A Aviral 0
    29 Nov 2022, 18:04

    @JonB Using
    && index.row() == 0
    also didn't worked. I think index contains the current item location in tree

    J Offline
    J Offline
    JonB
    wrote on 29 Nov 2022, 18:09 last edited by
    #13

    @Aviral-0
    Your QTreeView has a root index, the one you want a checkbox against, right? How did that get set? You want to know that index in the model code so that you can check for it your data() code, right?

    A 2 Replies Last reply 29 Nov 2022, 18:14
    1
    • J JonB
      29 Nov 2022, 18:09

      @Aviral-0
      Your QTreeView has a root index, the one you want a checkbox against, right? How did that get set? You want to know that index in the model code so that you can check for it your data() code, right?

      A Offline
      A Offline
      Aviral 0
      wrote on 29 Nov 2022, 18:14 last edited by
      #14

      @JonB Sorry Jon, I really didn't understand. I am very new to coding.
      In simple terms, yes I need checkbox only at root node.
      Should I send you my updated code?

      1 Reply Last reply
      0
      • J JonB
        29 Nov 2022, 18:09

        @Aviral-0
        Your QTreeView has a root index, the one you want a checkbox against, right? How did that get set? You want to know that index in the model code so that you can check for it your data() code, right?

        A Offline
        A Offline
        Aviral 0
        wrote on 29 Nov 2022, 18:25 last edited by
        #15

        @JonB Please have a look at it.
        https://github.com/aviralarpit/QTDomDoc.git

        J 1 Reply Last reply 29 Nov 2022, 18:37
        0
        • A Aviral 0
          29 Nov 2022, 18:25

          @JonB Please have a look at it.
          https://github.com/aviralarpit/QTDomDoc.git

          J Offline
          J Offline
          JonB
          wrote on 29 Nov 2022, 18:37 last edited by
          #16

          @Aviral-0
          class DomModel has QModelIndex rootIndex() const; (though can't see an implementation?) or DomItem *rootItem you can use to identify the root item.

          A 2 Replies Last reply 29 Nov 2022, 18:39
          1
          • J JonB
            29 Nov 2022, 18:37

            @Aviral-0
            class DomModel has QModelIndex rootIndex() const; (though can't see an implementation?) or DomItem *rootItem you can use to identify the root item.

            A Offline
            A Offline
            Aviral 0
            wrote on 29 Nov 2022, 18:39 last edited by
            #17

            @JonB I tried to use rootIndex() function but didn't understood how to.
            Its just declaration.

            J 1 Reply Last reply 29 Nov 2022, 18:44
            0
            • A Aviral 0
              29 Nov 2022, 18:39

              @JonB I tried to use rootIndex() function but didn't understood how to.
              Its just declaration.

              J Offline
              J Offline
              JonB
              wrote on 29 Nov 2022, 18:44 last edited by JonB
              #18

              @Aviral-0 Which is what I thought and why I suggested you try recognising if the item is the rootItem.

              1 Reply Last reply
              0
              • J JonB
                29 Nov 2022, 18:37

                @Aviral-0
                class DomModel has QModelIndex rootIndex() const; (though can't see an implementation?) or DomItem *rootItem you can use to identify the root item.

                A Offline
                A Offline
                Aviral 0
                wrote on 29 Nov 2022, 18:50 last edited by
                #19

                @JonB I know its alot to ask, but can you tell me what change should I do specifically to make it work. It will be so nice of you taking your time to help me. Thankyou Brother.

                J 1 Reply Last reply 29 Nov 2022, 18:56
                0
                • A Aviral 0
                  29 Nov 2022, 18:50

                  @JonB I know its alot to ask, but can you tell me what change should I do specifically to make it work. It will be so nice of you taking your time to help me. Thankyou Brother.

                  J Offline
                  J Offline
                  JonB
                  wrote on 29 Nov 2022, 18:56 last edited by
                  #20

                  @Aviral-0
                  I'm not your Brother!

                  I think you want in flags():

                      DomItem *item = static_cast<DomItem*>(index.internalPointer());
                      if (item == rootItem)
                          flags |= Qt::ItemIsUserCheckable;
                  

                  to make it so only the root item is checkable.

                  A 3 Replies Last reply 29 Nov 2022, 19:16
                  1
                  • J JonB
                    29 Nov 2022, 18:56

                    @Aviral-0
                    I'm not your Brother!

                    I think you want in flags():

                        DomItem *item = static_cast<DomItem*>(index.internalPointer());
                        if (item == rootItem)
                            flags |= Qt::ItemIsUserCheckable;
                    

                    to make it so only the root item is checkable.

                    A Offline
                    A Offline
                    Aviral 0
                    wrote on 29 Nov 2022, 19:16 last edited by
                    #21

                    @JonB Sorry, it didn't worked :'(

                    1 Reply Last reply
                    0
                    • J JonB
                      29 Nov 2022, 18:56

                      @Aviral-0
                      I'm not your Brother!

                      I think you want in flags():

                          DomItem *item = static_cast<DomItem*>(index.internalPointer());
                          if (item == rootItem)
                              flags |= Qt::ItemIsUserCheckable;
                      

                      to make it so only the root item is checkable.

                      A Offline
                      A Offline
                      Aviral 0
                      wrote on 30 Nov 2022, 04:16 last edited by
                      #22

                      @JonB I have solved this.
                      By using
                      DomModel::data(){
                      if ( role == Qt::CheckStateRole && !index.parent().isValid())
                      }

                      J 1 Reply Last reply 30 Nov 2022, 08:38
                      0
                      • J JonB
                        29 Nov 2022, 18:56

                        @Aviral-0
                        I'm not your Brother!

                        I think you want in flags():

                            DomItem *item = static_cast<DomItem*>(index.internalPointer());
                            if (item == rootItem)
                                flags |= Qt::ItemIsUserCheckable;
                        

                        to make it so only the root item is checkable.

                        A Offline
                        A Offline
                        Aviral 0
                        wrote on 30 Nov 2022, 05:25 last edited by
                        #23

                        @JonB Hi Jon, just one more thing needed.
                        I want to Highlight the items of tree which is selected, like when we do multiple selection using CTRL button, the selected lines should be in blue highlight or their fonts can done Bold to make selected Items looks different.
                        How should I do it? any idea ?

                        1 Reply Last reply
                        0
                        • A Aviral 0
                          30 Nov 2022, 04:16

                          @JonB I have solved this.
                          By using
                          DomModel::data(){
                          if ( role == Qt::CheckStateRole && !index.parent().isValid())
                          }

                          J Offline
                          J Offline
                          JonB
                          wrote on 30 Nov 2022, 08:38 last edited by
                          #24

                          @Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:

                          && !index.parent().isValid()

                          Ah, well done! I kind of thought that was for a child of the root element, and the root element was that parent. But I think it makes sense, I haven't actually ever used QTreeView I don't think.

                          Do you mean you only want to alter presentation when multiple items are selected, not just a single one? Normally you would do colour/font via stylesheet on selected items. But that won't distinguish multiple versus single selection. QTreeView::selectionModel/selectedIndexes() gives you which items/indexes are selected, that's all I know, somehow work from there?

                          A 1 Reply Last reply 30 Nov 2022, 09:14
                          1
                          • J JonB
                            30 Nov 2022, 08:38

                            @Aviral-0 said in How to Insert checkbox in root of Qtreeview. Treeview is created by using QDOMDocument reading XML file.:

                            && !index.parent().isValid()

                            Ah, well done! I kind of thought that was for a child of the root element, and the root element was that parent. But I think it makes sense, I haven't actually ever used QTreeView I don't think.

                            Do you mean you only want to alter presentation when multiple items are selected, not just a single one? Normally you would do colour/font via stylesheet on selected items. But that won't distinguish multiple versus single selection. QTreeView::selectionModel/selectedIndexes() gives you which items/indexes are selected, that's all I know, somehow work from there?

                            A Offline
                            A Offline
                            Aviral 0
                            wrote on 30 Nov 2022, 09:14 last edited by
                            #25

                            @JonB I am trying this piece of code:
                            const bool shouldBeBold = (index.column() == 0);
                            if (role == Qt::FontRole && shouldBeBold) {
                            QFont boldFont;
                            boldFont.setBold(true);
                            return boldFont;
                            } else {
                            return DomModel::data(index, role);
                            }

                            you repied to this code in past at someone elses pproblem.
                            but its not working

                            J 1 Reply Last reply 30 Nov 2022, 09:23
                            0
                            • A Aviral 0
                              30 Nov 2022, 09:14

                              @JonB I am trying this piece of code:
                              const bool shouldBeBold = (index.column() == 0);
                              if (role == Qt::FontRole && shouldBeBold) {
                              QFont boldFont;
                              boldFont.setBold(true);
                              return boldFont;
                              } else {
                              return DomModel::data(index, role);
                              }

                              you repied to this code in past at someone elses pproblem.
                              but its not working

                              J Offline
                              J Offline
                              JonB
                              wrote on 30 Nov 2022, 09:23 last edited by
                              #26

                              @Aviral-0
                              Debugging techniques:

                              • Put a qDebug() into your if statement, does it get hit at all?
                              • Set shouldBeBold = true unconditionally, does any items come up in bold?
                              • Try, say, role == Qt::ForegroundRole and return QColor(Qt::red), does that work in case it's some bold issue?
                              • If you are doing this on selected items it might be that selection sets font instead, I don't know.
                              A 1 Reply Last reply 30 Nov 2022, 13:00
                              0
                              • J JonB
                                30 Nov 2022, 09:23

                                @Aviral-0
                                Debugging techniques:

                                • Put a qDebug() into your if statement, does it get hit at all?
                                • Set shouldBeBold = true unconditionally, does any items come up in bold?
                                • Try, say, role == Qt::ForegroundRole and return QColor(Qt::red), does that work in case it's some bold issue?
                                • If you are doing this on selected items it might be that selection sets font instead, I don't know.
                                A Offline
                                A Offline
                                Aviral 0
                                wrote on 30 Nov 2022, 13:00 last edited by
                                #27

                                @JonB Nothing worked. any other ideas?

                                J 1 Reply Last reply 30 Nov 2022, 13:08
                                0
                                • A Aviral 0
                                  30 Nov 2022, 13:00

                                  @JonB Nothing worked. any other ideas?

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 30 Nov 2022, 13:08 last edited by
                                  #28

                                  @Aviral-0
                                  If "nothing worked" that means you are saying your code is not being called at all. I cannot help if you say that is the case.

                                  A 2 Replies Last reply 30 Nov 2022, 13:18
                                  0
                                  • J JonB
                                    30 Nov 2022, 13:08

                                    @Aviral-0
                                    If "nothing worked" that means you are saying your code is not being called at all. I cannot help if you say that is the case.

                                    A Offline
                                    A Offline
                                    Aviral 0
                                    wrote on 30 Nov 2022, 13:18 last edited by
                                    #29
                                    This post is deleted!
                                    1 Reply Last reply
                                    0
                                    • J JonB
                                      30 Nov 2022, 13:08

                                      @Aviral-0
                                      If "nothing worked" that means you are saying your code is not being called at all. I cannot help if you say that is the case.

                                      A Offline
                                      A Offline
                                      Aviral 0
                                      wrote on 30 Nov 2022, 16:09 last edited by
                                      #30

                                      @JonB
                                      Hi I solved it. I have added a function in mainwindow.cpp
                                      view->setSelectionMode(QAbstractItemView::MultiSelection);
                                      and I am able to select multiple items.
                                      thankyou for your patience and help!

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        Aviral 0
                                        wrote on 5 Dec 2022, 05:20 last edited by
                                        #31

                                        Hi, Hope you are having a good day.
                                        Now I need the checkboxes to all the parents of child.
                                        I have attached the photo attached of what I need.
                                        What to change in the if condition to make it happen?
                                        alt text

                                        QVariant DomModel::data(const QModelIndex &index, int role) const
                                        {
                                            if (!index.isValid())
                                                return QVariant();
                                        
                                            DomItem *item = static_cast<DomItem*>(index.internalPointer());
                                            const QDomNode node = item->node();
                                        
                                            //if ( role == Qt::CheckStateRole && !index.parent().isValid())
                                            //if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid())
                                            if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid() )
                                            {
                                                return static_cast< int >( item->isChecked() ? Qt::Checked : Qt::Unchecked );
                                        }
                                            if (role != Qt::DisplayRole)
                                                return QVariant();
                                        
                                            switch (index.column()) {
                                                case 0:
                                                    return node.nodeName();
                                                case 1:
                                                {
                                                    const QDomNamedNodeMap attributeMap = node.attributes();
                                                    QStringList attributes;
                                                    for (int i = 0; i < attributeMap.count(); ++i) {
                                                        QDomNode attribute = attributeMap.item(i);
                                                        attributes << attribute.nodeName() + "=\""
                                                                      + attribute.nodeValue() + '"';
                                                    }
                                                    return attributes.join(' ');
                                                }
                                                case 2:
                                                    return node.nodeValue().split('\n').join(' ');
                                                default:
                                                    break;
                                        
                                            }
                                        
                                            return item->data(index.column());
                                        }
                                        
                                        J 1 Reply Last reply 5 Dec 2022, 08:43
                                        0
                                        • A Aviral 0
                                          5 Dec 2022, 05:20

                                          Hi, Hope you are having a good day.
                                          Now I need the checkboxes to all the parents of child.
                                          I have attached the photo attached of what I need.
                                          What to change in the if condition to make it happen?
                                          alt text

                                          QVariant DomModel::data(const QModelIndex &index, int role) const
                                          {
                                              if (!index.isValid())
                                                  return QVariant();
                                          
                                              DomItem *item = static_cast<DomItem*>(index.internalPointer());
                                              const QDomNode node = item->node();
                                          
                                              //if ( role == Qt::CheckStateRole && !index.parent().isValid())
                                              //if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid())
                                              if ( role == Qt::CheckStateRole && index.column() == 0 && !index.parent().isValid() )
                                              {
                                                  return static_cast< int >( item->isChecked() ? Qt::Checked : Qt::Unchecked );
                                          }
                                              if (role != Qt::DisplayRole)
                                                  return QVariant();
                                          
                                              switch (index.column()) {
                                                  case 0:
                                                      return node.nodeName();
                                                  case 1:
                                                  {
                                                      const QDomNamedNodeMap attributeMap = node.attributes();
                                                      QStringList attributes;
                                                      for (int i = 0; i < attributeMap.count(); ++i) {
                                                          QDomNode attribute = attributeMap.item(i);
                                                          attributes << attribute.nodeName() + "=\""
                                                                        + attribute.nodeValue() + '"';
                                                      }
                                                      return attributes.join(' ');
                                                  }
                                                  case 2:
                                                      return node.nodeValue().split('\n').join(' ');
                                                  default:
                                                      break;
                                          
                                              }
                                          
                                              return item->data(index.column());
                                          }
                                          
                                          J Offline
                                          J Offline
                                          JonB
                                          wrote on 5 Dec 2022, 08:43 last edited by
                                          #32

                                          @Aviral-0
                                          So change the && !index.parent().isValid(), which you put in to make it root-only, to whatever it is you want to show checkboxes against?

                                          A 1 Reply Last reply 5 Dec 2022, 09:14
                                          0

                                          22/45

                                          30 Nov 2022, 04:16

                                          • Login

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