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. Checkable Qtreeview.
Qt 6.11 is out! See what's new in the release blog

Checkable Qtreeview.

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

    How ca I implement a checkable treeview?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Just attach a model with the Qt::ItemIsUserCheckable flag set for the indexes you want to be checkable

      "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

      B 1 Reply Last reply
      1
      • VRoninV VRonin

        Just attach a model with the Qt::ItemIsUserCheckable flag set for the indexes you want to be checkable

        B Offline
        B Offline
        BinuJanardhanan
        wrote on last edited by
        #3

        @VRonin could you please show me some example ?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4
          #include <QApplication>
          #include <QTreeWidget>
          
          int main(int argc, char *argv[])
          {
          
              QApplication app(argc,argv);
              QTreeWidget w;
              auto item1 = new QTreeWidgetItem(QStringList("0,0"));
              item1->setFlags(item1->flags() | Qt::ItemIsUserCheckable);
              item1->setData(0,Qt::CheckStateRole,Qt::Unchecked);
              w.addTopLevelItem(item1);
              auto item2 = new QTreeWidgetItem(QStringList("1,0"));
              item2->setFlags(item2->flags() | Qt::ItemIsUserCheckable);
              item1->setData(0,Qt::CheckStateRole,Qt::Unchecked);
              w.addTopLevelItem(item2);
              w.show();
              return app.exec();
          }
          

          I use QTreeWidget (which derives from QTreeView) for convenience here but you can use whatever combination of model/view you prefer

          "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
          2

          • Login

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