QFileSystemModel - Adding Custom Columns



  • Hey,

    This is a question I've found multiple references to in this forum and elsewhere, however most of the replies suggest to use QProxyModel to Add Custom Columns as per the requirements of the program as the simplest solution.

    QProxyModel in the Doc's Section states it is obsolete and not to use it. I'm just wondering is this still the only way to add custom columns to a QFileSystemModel.

    My Requirements are pretty simple with the custom column. I just need another column with a text indicator(a number or some sort of notification icon) I would populate the data of based on the QModelIndex of the QFileSystemModel.

    Thank you



  • I think what you should do is subclass QAbstractProxyModel (or maybe the new QIdentityProxyModel) instead of using/subclassing the obsolete QProxyModel class.



  • [quote author="ludde" date="1325689598"]I think what you should do is subclass QAbstractProxyModel (or maybe the new QIdentityProxyModel) instead of using/subclassing the obsolete QProxyModel class.[/quote]

    Thank you,

    Will give that a go.

    UPDATE:

    I tried using the QIdentityProxyModel via the following

    @
    drive_model_ = new QFileSystemModel(this);
    drive_proxy_ = new QIdentityProxyModel(this);
    drive_model_->setRootPath(cc_->MountPath());
    drive_proxy_->setSourceModel(drive_model_);

    QModelIndex drive_index = drive_model_->index(cc_->MountPath());
    @

    However on the last line I cant ofcourse retrieve the QModelIndex based on a QString path since QIdentityProxyModel does not support this and requires a row and column.

    Is there anyway I can get the corresponding index in the ProxyModel given the index in the drive_model_ and if not do i really need to go down this road for inserting a column into a QFileSystemModel and have some text in the new column which i provide?

    UPDATE:

    Soz abt that, Overlooked the mapToSource and mapFromSource functions :S



  • Am I doing something wrong here? (Cant seem to get the blank column inserted)

    @
    drive_proxy_->setSourceModel(drive_model_);
    QModelIndex drive_index = drive_model_->index(cc_->MountPath());
    int size_1 = drive_proxy_->columnCount();
    drive_proxy_->insertColumn(2, drive_proxy_->mapFromSource(drive_index));
    int size_2 = drive_proxy_->columnCount();
    @

    Both size_1 and size_2 are 4. The Function insertColumn returned false and stepping through the debug I cant see it fail at any stage and it tries to create a QModelIndex at a new position and returns false from there.

    UPDATE

    I see that QIdentityProxyModel just calls the QAbstractItemModel::insertColumns() Function which just returns false.

    I thought this class actually created the column and did not need a custom Implementation



  • Solved my issue of needing to add an extra column.

    I'm just going to post what I did incase someone needs the same later.

    I've subclassed QFileSystemModel and added one column to it called it "Notifications".

    The data Function which returns a random uint can just be made to return the data needed as per your requirement I guess.

    @
    int LifeDriveFileSystemModel::columnCount(const QModelIndex &parent) const {
    return (parent.column() > 0) ? 0 : 5;
    }

    Qt::ItemFlags LifeDriveFileSystemModel::flags(const QModelIndex &index) const {
    if (!index.isValid())
    return 0;
    if (index.column() == 4)
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    return QFileSystemModel::flags(index);
    }

    QVariant LifeDriveFileSystemModel::data(const QModelIndex &index,
    int role) const {
    if (!index.isValid())
    return QVariant();
    if (role == Qt::DisplayRole && index.column() == 4)
    return QString::number(RandomUint32() % 10);
    return QFileSystemModel::data(index, role);
    }

    QVariant LifeDriveFileSystemModel::headerData(int section,
    Qt::Orientation orientation,
    int role) const {
    if (section == 4 && orientation == Qt::Horizontal && role == Qt::DisplayRole)
    return tr("Notifications");
    return QFileSystemModel::headerData(section, orientation, role);
    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.