Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

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

    General and Desktop
    3
    3
    1332
    Loading More Posts
    • 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
      Qt Enthusiast last edited by Qt Enthusiast

      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 Reply Quote 0
      • sneubert
        sneubert last edited by

        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 Reply Quote 1
        • VRonin
          VRonin last edited by VRonin

              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 Reply Quote 1
          • First post
            Last post