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.
  • 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
        • J JonB
          5 Dec 2022, 08:43

          @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 Offline
          A Offline
          Aviral 0
          wrote on 5 Dec 2022, 09:14 last edited by
          #33

          @JonB Please look at the image I jhave attached!
          Image(https://wetransfer.com/downloads/a9b408bd97b18185f4fc64ac8053fbb520221205051935/8f1754)

          J 1 Reply Last reply 5 Dec 2022, 09:16
          0
          • A Aviral 0
            5 Dec 2022, 09:14

            @JonB Please look at the image I jhave attached!
            Image(https://wetransfer.com/downloads/a9b408bd97b18185f4fc64ac8053fbb520221205051935/8f1754)

            J Offline
            J Offline
            JonB
            wrote on 5 Dec 2022, 09:16 last edited by JonB 12 May 2022, 09:22
            #34

            @Aviral-0
            Then upload an image to your post (best, the post toolbar has "cloud" icon tooltip Upload Image), or at worst given a link to where the image can be seen. If you think I am going to download an image you want me to look at from a site , I am not.

            A 1 Reply Last reply 5 Dec 2022, 09:24
            0
            • J JonB
              5 Dec 2022, 09:16

              @Aviral-0
              Then upload an image to your post (best, the post toolbar has "cloud" icon tooltip Upload Image), or at worst given a link to where the image can be seen. If you think I am going to download an image you want me to look at from a site , I am not.

              A Offline
              A Offline
              Aviral 0
              wrote on 5 Dec 2022, 09:24 last edited by
              #35

              Screenshot (226).png

              J 1 Reply Last reply 5 Dec 2022, 09:28
              0
              • A Aviral 0
                5 Dec 2022, 09:24

                Screenshot (226).png

                J Offline
                J Offline
                JonB
                wrote on 5 Dec 2022, 09:28 last edited by
                #36

                @Aviral-0
                [I presume you also want a checkbox against the devices node.] So you want a checkbox against each node in the tree, but not against leaves? So check to see (in data()) whether the DomItem item for the index has any children or not.

                A 1 Reply Last reply 5 Dec 2022, 09:47
                0
                • J JonB
                  5 Dec 2022, 09:28

                  @Aviral-0
                  [I presume you also want a checkbox against the devices node.] So you want a checkbox against each node in the tree, but not against leaves? So check to see (in data()) whether the DomItem item for the index has any children or not.

                  A Offline
                  A Offline
                  Aviral 0
                  wrote on 5 Dec 2022, 09:47 last edited by
                  #37

                  @JonB Yes they have childItems

                  J 1 Reply Last reply 5 Dec 2022, 09:50
                  0
                  • A Aviral 0
                    5 Dec 2022, 09:47

                    @JonB Yes they have childItems

                    J Offline
                    J Offline
                    JonB
                    wrote on 5 Dec 2022, 09:50 last edited by
                    #38

                    @Aviral-0
                    I know! So test for that when deciding whether to request a checkbox or not, in place of your && !index.parent().isValid() which is currently restricting them to root-only node.

                    A 1 Reply Last reply 5 Dec 2022, 10:28
                    0
                    • J JonB
                      5 Dec 2022, 09:50

                      @Aviral-0
                      I know! So test for that when deciding whether to request a checkbox or not, in place of your && !index.parent().isValid() which is currently restricting them to root-only node.

                      A Offline
                      A Offline
                      Aviral 0
                      wrote on 5 Dec 2022, 10:28 last edited by
                      #39

                      @JonB
                      Yes, but removing that causes checkboxes at every line,
                      I only want at items which are root or parent to some items. Not on leafs

                      J 1 Reply Last reply 5 Dec 2022, 10:31
                      0
                      • A Aviral 0
                        5 Dec 2022, 10:28

                        @JonB
                        Yes, but removing that causes checkboxes at every line,
                        I only want at items which are root or parent to some items. Not on leafs

                        J Offline
                        J Offline
                        JonB
                        wrote on 5 Dec 2022, 10:31 last edited by
                        #40

                        @Aviral-0
                        You are ignoring what I am writing. I told you how to do that, you have not acted on it.

                        A 1 Reply Last reply 5 Dec 2022, 12:08
                        0
                        • J JonB
                          5 Dec 2022, 10:31

                          @Aviral-0
                          You are ignoring what I am writing. I told you how to do that, you have not acted on it.

                          A Offline
                          A Offline
                          Aviral 0
                          wrote on 5 Dec 2022, 12:08 last edited by Aviral 0 12 May 2022, 12:08
                          #41

                          @JonB I have tried it bro... I tried removing !index.parent.isvalid()
                          and placing some other functions.
                          I am not fully understanding of what you are suggesting. can you tell me what to write there?

                          Screenshot (227).png

                          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 )
                              if ( role == Qt::CheckStateRole && (index.column() == 0)  )
                              {
                                  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());
                          }
                          
                          
                          Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
                          {
                              if (!index.isValid())
                                  return Qt::NoItemFlags;
                          
                              Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
                          
                              if ( index.column() == 0 )
                                  flags |= Qt::ItemIsUserCheckable;
                              return flags;
                          }
                          //! [6]
                          
                          J 1 Reply Last reply 5 Dec 2022, 12:51
                          0
                          • A Aviral 0
                            5 Dec 2022, 12:08

                            @JonB I have tried it bro... I tried removing !index.parent.isvalid()
                            and placing some other functions.
                            I am not fully understanding of what you are suggesting. can you tell me what to write there?

                            Screenshot (227).png

                            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 )
                                if ( role == Qt::CheckStateRole && (index.column() == 0)  )
                                {
                                    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());
                            }
                            
                            
                            Qt::ItemFlags DomModel::flags(const QModelIndex &index) const
                            {
                                if (!index.isValid())
                                    return Qt::NoItemFlags;
                            
                                Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
                            
                                if ( index.column() == 0 )
                                    flags |= Qt::ItemIsUserCheckable;
                                return flags;
                            }
                            //! [6]
                            
                            J Offline
                            J Offline
                            JonB
                            wrote on 5 Dec 2022, 12:51 last edited by
                            #42

                            @Aviral-0
                            I am not your "bro"! You need to replace the original && !index.parent().isValid() with the condition for QDomNode node not having any children.

                            A 1 Reply Last reply 5 Dec 2022, 14:55
                            0
                            • J JonB
                              5 Dec 2022, 12:51

                              @Aviral-0
                              I am not your "bro"! You need to replace the original && !index.parent().isValid() with the condition for QDomNode node not having any children.

                              A Offline
                              A Offline
                              Aviral 0
                              wrote on 5 Dec 2022, 14:55 last edited by
                              #43

                              @JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!

                              J J 2 Replies Last reply 5 Dec 2022, 15:48
                              0
                              • A Aviral 0
                                5 Dec 2022, 14:55

                                @JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!

                                J Offline
                                J Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on 5 Dec 2022, 15:48 last edited by
                                #44

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

                                I didn't find any solutions to my problem from you!

                                What about "You need to replace the original && !index.parent().isValid() with the condition for QDomNode node not having any children." from @JonB - did you do that?

                                Please state polite...

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • A Aviral 0
                                  5 Dec 2022, 14:55

                                  @JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 5 Dec 2022, 16:05 last edited by JonB 12 May 2022, 16:05
                                  #45

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

                                  @JonB You don't know anything, just arrogant rude. I didn't find any solutions to my problem from you!

                                  Just wow! I have made over a dozen posts so far over a week trying to help you solve your issues. Told you at each stage what to do, and at least a couple of times what to do now. Why don't you have a look back up? Thanks for nothing, bro.

                                  1 Reply Last reply
                                  1

                                  39/45

                                  5 Dec 2022, 10:28

                                  • Login

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