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 make text Bold of the Item selected with Checkbox in QT?
Forum Updated to NodeBB v4.3 + New Features

How to make text Bold of the Item selected with Checkbox in QT?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 373 Views 3 Watching
  • 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.
  • Aviral 0A Offline
    Aviral 0A Offline
    Aviral 0
    wrote on last edited by
    #1

    I want to make checked Items in Bold Format in QT DOM Model.

    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.column() == 0) && hasChildren(index) )
            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());
    }
    

    Flag Function

    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;
    }
    

    setData Function

    bool DomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
        DomItem *item = static_cast<DomItem*>(index.internalPointer());
    
        if (index.column() == 0 ){
            if (role == Qt::EditRole) {
                return false;
            }
            if (role == Qt::CheckStateRole) {
                item->setChecked(value.toBool());
                emit dataChanged(index, index);
                return true;
            }
        }
        return QAbstractItemModel::setData(index, value, role);
    }
    

    Screenshot (228).png
    I need Fonts bold of the items which have checkbox is checked!

    JonBJ Aviral 0A 2 Replies Last reply
    0
    • Aviral 0A Aviral 0

      I want to make checked Items in Bold Format in QT DOM Model.

      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.column() == 0) && hasChildren(index) )
              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());
      }
      

      Flag Function

      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;
      }
      

      setData Function

      bool DomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
          DomItem *item = static_cast<DomItem*>(index.internalPointer());
      
          if (index.column() == 0 ){
              if (role == Qt::EditRole) {
                  return false;
              }
              if (role == Qt::CheckStateRole) {
                  item->setChecked(value.toBool());
                  emit dataChanged(index, index);
                  return true;
              }
          }
          return QAbstractItemModel::setData(index, value, role);
      }
      

      Screenshot (228).png
      I need Fonts bold of the items which have checkbox is checked!

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @Aviral-0 said in How to make text Bold of the Item selected with Checkbox in QT?:

      ... && hasChildren(index)

      Wow!

      Aviral 0A 1 Reply Last reply
      0
      • JonBJ JonB

        @Aviral-0 said in How to make text Bold of the Item selected with Checkbox in QT?:

        ... && hasChildren(index)

        Wow!

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

        @JonB yes it solved the problem, simple and easy, not complicating things. You can see the result in image

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

          @JonB yes it solved the problem, simple and easy, not complicating things. You can see the result in image

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

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

          So precisely what I explained to you to do.

          Aviral 0A 1 Reply Last reply
          0
          • JonBJ JonB

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

            So precisely what I explained to you to do.

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

            @JonB haha

            1 Reply Last reply
            0
            • Aviral 0A Aviral 0

              I want to make checked Items in Bold Format in QT DOM Model.

              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.column() == 0) && hasChildren(index) )
                      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());
              }
              

              Flag Function

              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;
              }
              

              setData Function

              bool DomModel::setData(const QModelIndex &index, const QVariant &value, int role) {
                  DomItem *item = static_cast<DomItem*>(index.internalPointer());
              
                  if (index.column() == 0 ){
                      if (role == Qt::EditRole) {
                          return false;
                      }
                      if (role == Qt::CheckStateRole) {
                          item->setChecked(value.toBool());
                          emit dataChanged(index, index);
                          return true;
                      }
                  }
                  return QAbstractItemModel::setData(index, value, role);
              }
              

              Screenshot (228).png
              I need Fonts bold of the items which have checkbox is checked!

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

              @Aviral-0
              So far i have tried to Implement
              if (role == Qt::FontRole && item->isChecked())
              return QFont::Bold;

              But its not working. Anyone Please help

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

                @Aviral-0
                So far i have tried to Implement
                if (role == Qt::FontRole && item->isChecked())
                return QFont::Bold;

                But its not working. Anyone Please help

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @Aviral-0

                QFont font;    // default font
                font.setBold(true);
                return font;
                
                1 Reply Last reply
                3

                • Login

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