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. Checkbox in Qtableview.

Checkbox in Qtableview.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 5 Posters 36.5k 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.
  • A Offline
    A Offline
    Anjan369
    wrote on last edited by
    #1

    i have created a Qtableview using Qabstracttablemodel. i want to add a checkbox to a column which user can operate. i set the flag to usercheckable. but i m not geting how user can interatct using the checkbox. means how to genrate a signal when it is checked/uncecked. and how i can catch the signal.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vittalonline
      wrote on last edited by
      #2

      it will works fine by using QTableWidget
      @
      QCheckBox *cb1..

      QTableWidgetItem *item = new QTableWidgetItem();
      item->data(Qt::CheckStateRole);
      item->setCheckState(Qt::Unchecked);
      tabWidget->setCellWidget(i,j,cb1);@

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anjan369
        wrote on last edited by
        #3

        thnx for reply.. we can do it by Qtablewidget, but is there any way to the same thing in Qtableview .as in my project iam using the model view.???

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SetBetterPass
          wrote on last edited by
          #4

          you can do something like this:
          @
          QStandardItem* item;
          item = new QStandardItem(true);
          item->setCheckable(true);
          item->setCheckState(Qt::Checked);
          model->setItem(x,y, item); //add to QAbstractItemModel to cell at x,y
          @
          to read from it anywhere in program use:
          @
          QModelIndex index;
          index= tableView->model()->index(x,y, QModelIndex());
          if(index.data(Qt::CheckStateRole) == Qt::Checked){
          //do something
          }
          @

          Hope this helps

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anjan369
            wrote on last edited by
            #5

            thnx for reply,,,here as per i tried model can be QAbstractItemModel *model = index.model();

            which doesnt have a function setItem ...can u be little more clear in which function i need to initialize my QStandardItem* item; (ie in data or flag).
            and even though my flag is set to QAbstractItemModel::flags(Index) | Qt::ItemIsUserCheckable..

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              It certainly is possible to do with your own models. Just be sure that you reimplement flags() to return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsEditable for your item. Also, data() needs to return a valid value for the Qt::CheckStateRole.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anjan369
                wrote on last edited by
                #7

                thx Andre for reply.i have already tried to reimplement my Flag(),,but i am not getting how to return the valid value in Qt::checkstaterole...
                if i return Qt::unchked or Qt::checked...it is fixed not user editable..
                can u help how can i return valid Qt::checkstaterole.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Well, it is obviously up to you to determine which value to return. The checked state must correspond with some value in your underlying data store, I'd assume. So, depending on the state of that store, return either Qt::Unchecked or Qt::Checked.

                  Also: don't forget to implement setData() as well. This method will be called whenever the user checks or unchecks the checkbox. Changing the state should result in an emit of the dataChanged() signal. If you don't implement it, the state of the checkbox won't change on clicking it.

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    redstoneleo
                    wrote on last edited by
                    #9

                    a minimized example
                    https://forum.qt.io/topic/26422/how-to-make-qabstracttablemodel-s-data-checkable/5

                    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