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 change the checkbox size of QFileSystemModel and custom checkbox shape
Forum Updated to NodeBB v4.3 + New Features

How to change the checkbox size of QFileSystemModel and custom checkbox shape

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 265 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 IknowQT
    #1

    6dfdbd32-137d-4f76-adcb-1eea9b1ac051-image.png
    a3f2a90f-95cf-4e86-ae08-882ebb9646db-image.png

    Declare it as subclassing in a class that inherits from QFileSystemModel and give it a checkbox property.
    The result was successful. However, it is a basic check box provided by qt and the size is also too small. I want to grow a checkbox and change it to a checkbox I have defined.

    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);
    };
    
    
    
    #include "usrFileSystemModel.h"
    
    usrFileSystemModel::usrFileSystemModel(QObject *parent)
    	: QFileSystemModel(parent)
    {
    	this->connect(this, SIGNAL(itemChecked(const QModelIndex&)), SLOT(onItemChecked(const QModelIndex&)));
    }
    
    usrFileSystemModel::~usrFileSystemModel()
    {
    }
    
    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;
            }
        }
    }
    
    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