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.
Forum Updated to NodeBB v4.3 + New Features

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 6.1k 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.
  • JonBJ JonB

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

    still not able to make check boxes work

    What exactly does this mean? Do you get to see any checkboxes? You do but they are not checkable? Or what?

    make checkboxes at the root only.

    I do not see anything in your data() method, CheckStateRole case, which attempts to only return the checked state for the root item, rather than for all/any item. I suggested you look at QAbstractItemView::rootIndex(), but I don't see that or any equivalent code.

    Aviral 0A Offline
    Aviral 0A Offline
    Aviral 0
    wrote on last edited by
    #8

    @JonB Yes, the checkbox appeared but not working as check or unchecked.

    JonBJ 1 Reply Last reply
    0
    • Aviral 0A Aviral 0

      @JonB Yes, the checkbox appeared but not working as check or unchecked.

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

      @Aviral-0
      Your DomModel::data() method has correct code to look at item->isChecked() to see whether to show checked/unchecked. But I do not see anywhere in your code which sets any DomItem (or its QDomNode returned by node()) to record that it has been checked. You have to store that somewhere. And if DomModel::data() looks at item->isChecked() for CheckStateRole I think I would expect you to have a corresponding setData() with case for CheckStateRole which calls an item->setChecked().

      Aviral 0A 1 Reply Last reply
      1
      • JonBJ JonB

        @Aviral-0
        Your DomModel::data() method has correct code to look at item->isChecked() to see whether to show checked/unchecked. But I do not see anywhere in your code which sets any DomItem (or its QDomNode returned by node()) to record that it has been checked. You have to store that somewhere. And if DomModel::data() looks at item->isChecked() for CheckStateRole I think I would expect you to have a corresponding setData() with case for CheckStateRole which calls an item->setChecked().

        Aviral 0A Offline
        Aviral 0A Offline
        Aviral 0
        wrote on last edited by
        #10

        @JonB Thankyou, using setData() method the items are now checkable.
        Just one thing I am not understanding. you said about
        QModelIndex QAbstractItemView::rootIndex() const
        I am not able to understand how to use it and where to use it.
        as on website its not indepth elaboration about it.

        JonBJ 1 Reply Last reply
        0
        • Aviral 0A Aviral 0

          @JonB Thankyou, using setData() method the items are now checkable.
          Just one thing I am not understanding. you said about
          QModelIndex QAbstractItemView::rootIndex() const
          I am not able to understand how to use it and where to use it.
          as on website its not indepth elaboration about it.

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

          @Aviral-0

          Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
          {
              if ( index.column() == 0 )
                  flags |= Qt::ItemIsUserCheckable;
          

          You said, I believe, that you only want a checkbox if it is the root index. You want to know whether index is the root index here. I realize now that QModelIndex QAbstractItemView::rootIndex() const is a method of the QTreeView and not available in model flags code. Is the root index row 0 in the model (I don't know)? So you would need maybe && index.row() == 0?

          Aviral 0A 1 Reply Last reply
          1
          • JonBJ JonB

            @Aviral-0

            Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
            {
                if ( index.column() == 0 )
                    flags |= Qt::ItemIsUserCheckable;
            

            You said, I believe, that you only want a checkbox if it is the root index. You want to know whether index is the root index here. I realize now that QModelIndex QAbstractItemView::rootIndex() const is a method of the QTreeView and not available in model flags code. Is the root index row 0 in the model (I don't know)? So you would need maybe && index.row() == 0?

            Aviral 0A Offline
            Aviral 0A Offline
            Aviral 0
            wrote on last edited by
            #12

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

            JonBJ 1 Reply Last reply
            0
            • Aviral 0A Aviral 0

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

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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?

              Aviral 0A 2 Replies Last reply
              1
              • JonBJ JonB

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

                Aviral 0A Offline
                Aviral 0A Offline
                Aviral 0
                wrote on 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
                • JonBJ JonB

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

                  Aviral 0A Offline
                  Aviral 0A Offline
                  Aviral 0
                  wrote on last edited by
                  #15

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

                  JonBJ 1 Reply Last reply
                  0
                  • Aviral 0A Aviral 0

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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.

                    Aviral 0A 2 Replies Last reply
                    1
                    • JonBJ JonB

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

                      Aviral 0A Offline
                      Aviral 0A Offline
                      Aviral 0
                      wrote on last edited by
                      #17

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

                      JonBJ 1 Reply Last reply
                      0
                      • Aviral 0A Aviral 0

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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
                        • JonBJ JonB

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

                          Aviral 0A Offline
                          Aviral 0A Offline
                          Aviral 0
                          wrote on 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.

                          JonBJ 1 Reply Last reply
                          0
                          • Aviral 0A Aviral 0

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

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on 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.

                            Aviral 0A 3 Replies Last reply
                            1
                            • JonBJ JonB

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

                              Aviral 0A Offline
                              Aviral 0A Offline
                              Aviral 0
                              wrote on last edited by
                              #21

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

                              1 Reply Last reply
                              0
                              • JonBJ JonB

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

                                Aviral 0A Offline
                                Aviral 0A Offline
                                Aviral 0
                                wrote on last edited by
                                #22

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

                                JonBJ 1 Reply Last reply
                                0
                                • JonBJ JonB

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

                                  Aviral 0A Offline
                                  Aviral 0A Offline
                                  Aviral 0
                                  wrote on 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
                                  • Aviral 0A Aviral 0

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

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 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?

                                    Aviral 0A 1 Reply Last reply
                                    1
                                    • JonBJ JonB

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

                                      Aviral 0A Offline
                                      Aviral 0A Offline
                                      Aviral 0
                                      wrote on 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

                                      JonBJ 1 Reply Last reply
                                      0
                                      • Aviral 0A Aviral 0

                                        @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

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on 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.
                                        Aviral 0A 1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @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.
                                          Aviral 0A Offline
                                          Aviral 0A Offline
                                          Aviral 0
                                          wrote on last edited by
                                          #27

                                          @JonB Nothing worked. any other ideas?

                                          JonBJ 1 Reply Last reply
                                          0

                                          • Login

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