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. How to enhace the left most button of QTableView with QAbstractTableModel to have a checkbox functionality
QtWS25 Last Chance

How to enhace the left most button of QTableView with QAbstractTableModel to have a checkbox functionality

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.6k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by Qt Enthusiast
    #1

    How to enhance the leftmost button of QTableView with QAbstractTableModel

    I have QTableView and it is using QAbstractTableModel . When I click on LeftMost button it selects All the rows in QtableView like microsoft Excel .

    Is it possible to enhance this corner most button as check box and when I click on this checkbox then it selects All the all the rows on this item and when I unlick it deselects all the items in the QtableView

    1 Reply Last reply
    0
    • sneubertS Offline
      sneubertS Offline
      sneubert
      wrote on last edited by
      #2

      Hi,

      the most left button is the row header, where you can only set text, font, etc. in QAbstractItemModel::headerData. My suggestion is to add a column to you model with

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

      to display a user clickable checkbox in the first column. You implementation of data has to return the checked state

      QVariant QAbstractItemModel::data(const QModelIndex &index, int role) const
      {
               /* your return for Qt:DisplayRole */
      
              if(role == Qt::CheckStateRole)
              {
                  if(index.column() == 0)
                  {
                      return //your check state for index.row()
                  }
              }
          }
          return QVariant();
      }
      

      You can than use setData of your model to select / unselect the entire row if the user checks / unchecks the checkbox

      bool QAbstractItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
      {
          if(index.column() == 0 && role == Qt::CheckStateRole)
          {
              /* save checked state and select / deseclect row */
              return true;
          }
          return false;
      }
      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3
            QAbstractButton *cornerButton = tableWidget->findChild<QAbstractButton*>();
            if (cornerButton) 
            {
               cornerButton->disconnect();
               connect(cornerButton, SIGNAL(clicked()), ..., ...);
            }
        

        Source: http://www.qtcentre.org/threads/31156-Cutom-signal-on-mouse-click-at-top-left-corner-of-QTableWidget

        "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

        • Login

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