Forum
Sign Up
× DataGrid for Qt.C++

Custom Sort( Filesystem Sorting )

1 week 5 days ago #1 by GYEYOUNG TNI
I want to implement sorting like QFileSystemModel via QTitanDataGrid . The model requires that directories are displayed first, but QTitanDataGrid cannot be implemented because it does not use the sort function of QAbstractItemModel.

the model, currently using, being used is a model that inherits QAbstractItemModel , items are stored as QVector<Item> , and it is possible to determine whether the item is a directory by a bool value.

struct Item
{
bool isDir;
QString sName;
}

please quick reply!!

Please Log in or Create an account to join the conversation.

More
1 week 4 days ago - 1 week 4 days ago #2 by Developer Machines
The grid has a built-in mechanism for sorting by column (or by multiple columns). Also this sorting algorithm is used for grouping. You don't need to have sorting support inside the Qt model.
To sort by a column programmatically, use this method:
Qtitan::GridTableColumn* column = static_cast<Qtitan::GridTableColumn*>(view->getColumn(0));
column->setSortOrder(Qtitan::SortAscending);

You can also specify any role that will be used to get the value from the model by which sorting will be performed (By default Qt::DisplayRole is used):
Qtitan::GridTableColumn* column = static_cast<Qtitan::GridTableColumn*>(view->getColumn(0));
column->dataBinding()->setSortRole(Qt::CustomRole);
Last edit: 1 week 4 days ago by Developer Machines.

Please Log in or Create an account to join the conversation.

More
1 week 3 days ago #3 by GYEYOUNG TNI
hmm... i want to implement sorting like windows explorer using qtitandatagrid.

windows explorer sort filename column, first directory, second files.

but qtitandatagrid cannot this.

Please Log in or Create an account to join the conversation.

More
1 week 2 days ago #4 by Developer Machines
Indeed, when you click on the filename column, the sorting will be by filename. if you also need to sort by file type (directory or file), then you need to multi-sort by file name and file type (two columns).
To do this, hold down the Shift key and click on the second column. However, there is one problem in the standard Qt model - QFileSystemModel, a string for the file type is used, which contains a description by file extension. Below is the code that fixes this, just add these changes to Grid_FileBrowser.exe example, i have attachned the full project here -

File Attachment:

File Name: FileBrowser.zip
File Size:4 KB
:
class MyFileSystemModel : public QFileSystemModel
{
public:
    enum Roles {
        FileTypeRole = QFileSystemModel::FilePermissions + 1,
    };
    MyFileSystemModel(QObject* parent = nullptr) : QFileSystemModel(parent) {}
    QVariant data(const QModelIndex& index, int role) const
    {
        if (!index.isValid() || index.model() != this)
            return QVariant();
        if (role == FileTypeRole && index.column() == 2)
        {
            QString s = QFileSystemModel::data(index, Qt::DisplayRole).toString();
            if (s != "File Folder")
                s = "File";
            return s;
        }
        return QFileSystemModel::data(index, role);
    }
};

Window::Window()
: DemoMainWindow(QStringLiteral("QtitanDataGrid"), QStringLiteral(QTN_VERSION_DATAGRID_STR), tr("Qtitan::TreeGrid File Browser"))
{

............... 
    QFileSystemModel* fileSystem = new MyFileSystemModel();
    fileSystem->setRootPath(QLatin1String("C:/"));
    view->setModel(fileSystem);
....................

    column = static_cast<Qtitan::GridBandedTableColumn*>(view->getColumn(2));
    column->setBandIndex(detailsBand->index());
    column->dataBinding()->setSortRole((Qt::ItemDataRole)MyFileSystemModel::FileTypeRole);
......................}




Or another option, as mentioned above, you can make a Custom Role for the filename column and return a value so that it takes into account the file name and type. In this case clicking on filename will sort by name and type.
Attachments:

Please Log in or Create an account to join the conversation.

More
  • Not Allowed: to create new topic.
  • Not Allowed: to reply.
  • Not Allowed: to edit your message.
Moderators: Developer Machines
Time to create page: 0.515 seconds

Developer Newsletter

Join our Developer Machines newsletter to get informed on all the latest releases of the commercial components for Qt.C++, Delphi FireMonkey, updates and general knowledges.

Quick Support

Should you need any additional information about our products or licensing, please contact us at the following email addresses:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

  • This email address is being protected from spambots. You need JavaScript enabled to view it.

Get in Touch

If you would like to purchase our products or services, but don’t know how to do it the right way, please feel free to contact us:

  • This email address is being protected from spambots. You need JavaScript enabled to view it.( any questions related to our products or services )
  • This email address is being protected from spambots. You need JavaScript enabled to view it.( questions related to licensing )