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. I want to put a checkbox when working with treeview using QFileSystemModel.
Forum Updated to NodeBB v4.3 + New Features

I want to put a checkbox when working with treeview using QFileSystemModel.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 378 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    There is a custom class that inherits QFileSystemModel class.
    In conjunction with treeview, the folder is displayed in the loop path.

    I want to display a checkbox in the ui, how can I do that?

    JonBJ 1 Reply Last reply
    0
    • I IknowQT

      @JonB

      To change the row size of the table view, use setitemdelegate to change the size.

      Crash when subclassing in filesystemmodel.
      I am getting an error in sizeHint of the itemdelegate class.

      QSize usrItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
      {
      	QSize oSize = QItemDelegate::sizeHint(option, index);
      
      	if (m_nHeightSize != -1)
      	{
      		// Set tree item height.
      		oSize.setHeight(m_nHeightSize);
      	}
      
      	return oSize;
      }
      
      

      980aa194-26ca-4c11-a0a1-3718f6e8779b-image.png

      Looking at the index, it seems that i is a garbage value.

      QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const
      {
      	if (role == Qt::CheckStateRole)
      	{
      		//this->checkIndex();
      	}
      	else
      	{
      		return QFileSystemModel::data(index, role);
      	}
      }
      
      bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role)
      {
      	return false;
      }
      
      Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const
      {
      	return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
      }
      
      
      I Offline
      I Offline
      IknowQT
      wrote on last edited by
      #4

      @IknowQT

      
      class usrFileSystemModel : public QFileSystemModel
      {
      	Q_OBJECT
      
      
      private:
      	mutable QMap <qint64, Qt::CheckState> m_CheckedItems;
      
      public:
      	usrFileSystemModel(QObject *parent);
      	~usrFileSystemModel();
      
      
      	// [ SubClassing ]
      	virtual bool			setData(const QModelIndex& index, const QVariant& value, int role);
      	virtual Qt::ItemFlags	flags(const QModelIndex& index) const;
      	virtual QVariant		data(const QModelIndex& index, int role)const;
      
      signals:
      	void					itemChecked(const QModelIndex&);
      
      
      protected slots:
      	void onItemChecked(const QModelIndex& index);
      
      
      };
      
      bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role)
      {
          if (role == Qt::CheckStateRole && index.column() == 0) {
      
              m_CheckedItems[index.internalId()] = static_cast<Qt::CheckState>(value.toInt());
              emit itemChecked(index);
              emit dataChanged(index, index.sibling(0, 0));
              return true;
          }
      
          return QFileSystemModel::setData(index, value, role);
      }
      
      Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const
      {
          return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
      }
      
      QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const
      {
          if (role == Qt::CheckStateRole && index.column() == 0) 
              return QVariant(m_CheckedItems[index.internalId()]);
      
          return QFileSystemModel::data(index, role);
      }
      
      void usrFileSystemModel::onItemChecked(const QModelIndex& index)
      {
          Qt::CheckState state = m_CheckedItems[index.internalId()];
          fetchMore(index);
         
          if (state == Qt::Checked || state == Qt::Unchecked) 
          {
              for (int i = 0; i < rowCount(index); i++) 
              {
                  QModelIndex child = index.child(i, 0);
                  if (m_CheckedItems[child.internalId()] != state)
                      setData(child, state, Qt::CheckStateRole);
              }
          }
      
          QModelIndex parent = index.parent();
          if (parent.isValid()) 
          {
              state = m_CheckedItems[parent.child(0, 0).internalId()];
              if (state == Qt::PartiallyChecked)
                  m_CheckedItems[parent.internalId()] = state;
              else 
              {
                  int i = 1;
                  while (i < rowCount(parent) && m_CheckedItems[parent.child(i, 0).internalId()] == state)
                      i++;
                  if (i != rowCount(index))
                      state = Qt::PartiallyChecked;
      
                  m_CheckedItems[parent.internalId()] = state;
              }
          }
      }
      

      solved

      1 Reply Last reply
      0
      • I IknowQT

        There is a custom class that inherits QFileSystemModel class.
        In conjunction with treeview, the folder is displayed in the loop path.

        I want to display a checkbox in the ui, how can I do that?

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

        @IknowQT
        If you Google for qfilesystemmodel checkbox you will find several discussions/examples.

        I 1 Reply Last reply
        0
        • JonBJ JonB

          @IknowQT
          If you Google for qfilesystemmodel checkbox you will find several discussions/examples.

          I Offline
          I Offline
          IknowQT
          wrote on last edited by
          #3

          @JonB

          To change the row size of the table view, use setitemdelegate to change the size.

          Crash when subclassing in filesystemmodel.
          I am getting an error in sizeHint of the itemdelegate class.

          QSize usrItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
          {
          	QSize oSize = QItemDelegate::sizeHint(option, index);
          
          	if (m_nHeightSize != -1)
          	{
          		// Set tree item height.
          		oSize.setHeight(m_nHeightSize);
          	}
          
          	return oSize;
          }
          
          

          980aa194-26ca-4c11-a0a1-3718f6e8779b-image.png

          Looking at the index, it seems that i is a garbage value.

          QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const
          {
          	if (role == Qt::CheckStateRole)
          	{
          		//this->checkIndex();
          	}
          	else
          	{
          		return QFileSystemModel::data(index, role);
          	}
          }
          
          bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role)
          {
          	return false;
          }
          
          Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const
          {
          	return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
          }
          
          
          I 1 Reply Last reply
          0
          • I IknowQT

            @JonB

            To change the row size of the table view, use setitemdelegate to change the size.

            Crash when subclassing in filesystemmodel.
            I am getting an error in sizeHint of the itemdelegate class.

            QSize usrItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
            {
            	QSize oSize = QItemDelegate::sizeHint(option, index);
            
            	if (m_nHeightSize != -1)
            	{
            		// Set tree item height.
            		oSize.setHeight(m_nHeightSize);
            	}
            
            	return oSize;
            }
            
            

            980aa194-26ca-4c11-a0a1-3718f6e8779b-image.png

            Looking at the index, it seems that i is a garbage value.

            QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const
            {
            	if (role == Qt::CheckStateRole)
            	{
            		//this->checkIndex();
            	}
            	else
            	{
            		return QFileSystemModel::data(index, role);
            	}
            }
            
            bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role)
            {
            	return false;
            }
            
            Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const
            {
            	return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
            }
            
            
            I Offline
            I Offline
            IknowQT
            wrote on last edited by
            #4

            @IknowQT

            
            class usrFileSystemModel : public QFileSystemModel
            {
            	Q_OBJECT
            
            
            private:
            	mutable QMap <qint64, Qt::CheckState> m_CheckedItems;
            
            public:
            	usrFileSystemModel(QObject *parent);
            	~usrFileSystemModel();
            
            
            	// [ SubClassing ]
            	virtual bool			setData(const QModelIndex& index, const QVariant& value, int role);
            	virtual Qt::ItemFlags	flags(const QModelIndex& index) const;
            	virtual QVariant		data(const QModelIndex& index, int role)const;
            
            signals:
            	void					itemChecked(const QModelIndex&);
            
            
            protected slots:
            	void onItemChecked(const QModelIndex& index);
            
            
            };
            
            bool usrFileSystemModel::setData(const QModelIndex& index, const QVariant& value, int role)
            {
                if (role == Qt::CheckStateRole && index.column() == 0) {
            
                    m_CheckedItems[index.internalId()] = static_cast<Qt::CheckState>(value.toInt());
                    emit itemChecked(index);
                    emit dataChanged(index, index.sibling(0, 0));
                    return true;
                }
            
                return QFileSystemModel::setData(index, value, role);
            }
            
            Qt::ItemFlags usrFileSystemModel::flags(const QModelIndex& index) const
            {
                return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable;
            }
            
            QVariant usrFileSystemModel::data(const QModelIndex& index, int role) const
            {
                if (role == Qt::CheckStateRole && index.column() == 0) 
                    return QVariant(m_CheckedItems[index.internalId()]);
            
                return QFileSystemModel::data(index, role);
            }
            
            void usrFileSystemModel::onItemChecked(const QModelIndex& index)
            {
                Qt::CheckState state = m_CheckedItems[index.internalId()];
                fetchMore(index);
               
                if (state == Qt::Checked || state == Qt::Unchecked) 
                {
                    for (int i = 0; i < rowCount(index); i++) 
                    {
                        QModelIndex child = index.child(i, 0);
                        if (m_CheckedItems[child.internalId()] != state)
                            setData(child, state, Qt::CheckStateRole);
                    }
                }
            
                QModelIndex parent = index.parent();
                if (parent.isValid()) 
                {
                    state = m_CheckedItems[parent.child(0, 0).internalId()];
                    if (state == Qt::PartiallyChecked)
                        m_CheckedItems[parent.internalId()] = state;
                    else 
                    {
                        int i = 1;
                        while (i < rowCount(parent) && m_CheckedItems[parent.child(i, 0).internalId()] == state)
                            i++;
                        if (i != rowCount(index))
                            state = Qt::PartiallyChecked;
            
                        m_CheckedItems[parent.internalId()] = state;
                    }
                }
            }
            

            solved

            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