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 box with QAbstractTableModel implementation

Checkable box with QAbstractTableModel implementation

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.5k Views 1 Watching
  • 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.
  • HunterMetcalfeH Offline
    HunterMetcalfeH Offline
    HunterMetcalfe
    wrote on last edited by
    #1

    I need to add a checkbox to my TableView through my model. I've already added the

    if ( role == Qt::CheckStateRole && index.column() == CHECKBOX_COL )
    {
        retval = Qt::Unchecked;
    }
    

    I've also attempted the setDataFunction. However, I cannot check the checkbox. I was wondering if anyone had an example where you can check rows of data in a TableView and where I'm able to capture the check or uncheck via a signal. I'll also be implementing check all and uncheck all functionality via buttons; however, would like to get the check buttons working properly first.

    Thanks!

    raven-worxR VRoninV 2 Replies Last reply
    0
    • HunterMetcalfeH HunterMetcalfe

      I need to add a checkbox to my TableView through my model. I've already added the

      if ( role == Qt::CheckStateRole && index.column() == CHECKBOX_COL )
      {
          retval = Qt::Unchecked;
      }
      

      I've also attempted the setDataFunction. However, I cannot check the checkbox. I was wondering if anyone had an example where you can check rows of data in a TableView and where I'm able to capture the check or uncheck via a signal. I'll also be implementing check all and uncheck all functionality via buttons; however, would like to get the check buttons working properly first.

      Thanks!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @HunterMetcalfe
      did you forget to add the Qt::ItemIsUserCheckable in the model's flag() implementation? (For the relevant model index)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • HunterMetcalfeH HunterMetcalfe

        I need to add a checkbox to my TableView through my model. I've already added the

        if ( role == Qt::CheckStateRole && index.column() == CHECKBOX_COL )
        {
            retval = Qt::Unchecked;
        }
        

        I've also attempted the setDataFunction. However, I cannot check the checkbox. I was wondering if anyone had an example where you can check rows of data in a TableView and where I'm able to capture the check or uncheck via a signal. I'll also be implementing check all and uncheck all functionality via buttons; however, would like to get the check buttons working properly first.

        Thanks!

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @HunterMetcalfe said in Checkable box with QAbstractTableModel implementation:

        where I'm able to capture the check or uncheck via a signal

        If you are implementing your own model add the optional const QVector<int> &roles as last argument to the dataChanged() signal and in the slot check that roles.contains(Qt::CheckStateRole)

        Otherwise.

        https://forum.qt.io/topic/80337/checkable-treeview-not-support-for-check-and-uncheck/4

        replace

        const int numRows = model->rowCount(idx);
        for(int i=0;i<numRows ;++i)
        model->setData(model->index(i,idx.column(),idx),newCheck,Qt::CheckStateRole);
        

        with emit checkChanged(idx);

        "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
        3
        • HunterMetcalfeH Offline
          HunterMetcalfeH Offline
          HunterMetcalfe
          wrote on last edited by
          #4

          Thanks for the replies! I will try exactly this. I apologize for the delay. Many other projects to be completed.

          1 Reply Last reply
          1

          • Login

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