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. QTableView not displaying check box for column zero
Forum Updated to NodeBB v4.3 + New Features

QTableView not displaying check box for column zero

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 397 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    In my TableModel I have defined a flags() mf :

            Qt::ItemFlags flags(const QModelIndex& index) const override
            {
                auto flags = QAbstractTableModel::flags(index);
                if (0 == index.column()) flags |= Qt::ItemIsUserCheckable;
                return flags;
            }
    

    in my data() mf I have:

            if (role == Qt::CheckStateRole)
            {
                if (0 == index.column())
                    return file.m_bChecked;
                else return QVariant();
    
            }
    

    where m_bChecked is of type QT::CheckState

    But I never see a check box added to column 0?

    Did I miss some crucial step?

    Thanks
    David

    1 Reply Last reply
    0
    • JonBJ JonB

      @Perdrix Check it hits your setData() override, and what happens there?

      PerdrixP Offline
      PerdrixP Offline
      Perdrix
      wrote on last edited by Perdrix
      #5

      @JonB Thanks for that pointer ... I'll look right away

      The code now reads:

                  else if (Qt::CheckStateRole == role && 0 == index.column())
                  {
                      file.m_bChecked = static_cast<Qt::CheckState>(value.toInt());
                      emit dataChanged(index, index);
                      return true;
                  }
      

      which seems to fix the problem ...

      Thanks a lot

      1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Put a debug statement in your data() / Qt::CheckStateRole and see if you get there. Also make sure m_bChecked is really of type Qt::CheckState.
        The flags() is only needed when you want the data to be modified by the user ('UserCheckable')

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • PerdrixP Offline
          PerdrixP Offline
          Perdrix
          wrote on last edited by
          #3

          OK that found the problem quickly enough - there was a spurious return QVariant() before the check for CheckStateRole. <BLUSH>

          However, now that's fixed, I cannot change the value of the check box by interacting with it - do I need to write some extra code to allow user interaction with that check box?

          Thanks
          David

          JonBJ 1 Reply Last reply
          0
          • PerdrixP Perdrix

            OK that found the problem quickly enough - there was a spurious return QVariant() before the check for CheckStateRole. <BLUSH>

            However, now that's fixed, I cannot change the value of the check box by interacting with it - do I need to write some extra code to allow user interaction with that check box?

            Thanks
            David

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Perdrix Check it hits your setData() override, and what happens there?

            PerdrixP 1 Reply Last reply
            1
            • JonBJ JonB

              @Perdrix Check it hits your setData() override, and what happens there?

              PerdrixP Offline
              PerdrixP Offline
              Perdrix
              wrote on last edited by Perdrix
              #5

              @JonB Thanks for that pointer ... I'll look right away

              The code now reads:

                          else if (Qt::CheckStateRole == role && 0 == index.column())
                          {
                              file.m_bChecked = static_cast<Qt::CheckState>(value.toInt());
                              emit dataChanged(index, index);
                              return true;
                          }
              

              which seems to fix the problem ...

              Thanks a lot

              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