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 am trying to use a checkbox after linking listview and model.
Forum Updated to NodeBB v4.3 + New Features

I am trying to use a checkbox after linking listview and model.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 215 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 12 Apr 2022, 04:26 last edited by
    #1

    I would like to use qfilesysystemmodel to represent files in a specific path as a listview.

    But I want to make a checkbox in the listview.
    I can use checkable in Standarditem, but I want to put a checkbox I want to make instead of the default checkbox. How can I access it?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 12 Apr 2022, 09:12 last edited by VRonin
      #2

      You can use RoleMaskProxyModel from this library (I already pointed to it in another of your questions, looks like we had to tackle similar problems)

      This is a working example:

      #include <RoleMaskProxyModel>
      #include <QFileSystemModel>
      #include <QStandardPaths>
      #include <QTreeView>
      #include <QApplication>
      
      // we create a small subclass of RoleMaskProxyModel
      class CheckableProxy : public RoleMaskProxyModel
      {
      public:
          explicit CheckableProxy(QObject *parent = nullptr)
              : RoleMaskProxyModel(parent)
          {
              // we will intercept all data in Qt::CheckStateRole and handle it in the proxy rather than passing it on to the source model
              addMaskedRole(Qt::CheckStateRole);
      
              // If we wanted checkboxes in all columns it would just be sufficient to disable the transparency of the proxy model
              // and assign Qt::Unchecked as the default value to make the checkbox appear
              // setTransparentIfEmpty(false);
              // setMaskedRoleDefaultValue(Qt::CheckStateRole, Qt::Unchecked);
          }
          // since we want the checkbox only on the first column we can reimplement data to return the default Qt::Unchecked if
          // no check state is set.
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
          {
              const QVariant baseData = RoleMaskProxyModel::data(index, role);
              if (role == Qt::CheckStateRole && index.isValid() && index.column() == 0 && !baseData.isValid())
                  return Qt::Unchecked;
              return baseData;
          }
      #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
          // same as above but using multiData in Qt6
          void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override
          {
              RoleMaskProxyModel::multiData(index, roleDataSpan);
              for (QModelRoleData &roleData : roleDataSpan) {
                  if (roleData.role() == Qt::CheckStateRole && index.column() == 0 && !roleData.data().isValid())
                      roleData.setData(Qt::Unchecked);
              }
          }
      #endif
          // we override the flags method to make the user able to interact with the checkbox
          Qt::ItemFlags flags(const QModelIndex &index) const override
          {
              if (index.isValid() && index.column() == 0)
                  return RoleMaskProxyModel::flags(index) | Qt::ItemIsUserCheckable;
              return RoleMaskProxyModel::flags(index);
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          // Create the source model
          QFileSystemModel baseModel;
      
          // create the proxy model
          CheckableProxy checkableProxy;
          // set the source model
          checkableProxy.setSourceModel(&baseModel);
      
          // we load a folder in the QFileSystemModel
          const QStringList docsLocations = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation);
          baseModel.setRootPath(docsLocations.first());
      
          // we display the proxy model in a tree view
          QTreeView mainView;
          mainView.setWindowTitle(QStringLiteral("RoleMaskProxyModel Example - Checkable QFileSystemModel"));
          mainView.setModel(&checkableProxy);
          mainView.show();
          return app.exec();
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1

      1/2

      12 Apr 2022, 04:26

      • Login

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