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 reference a indexWidget in a QTableView
QtWS25 Last Chance

How to reference a indexWidget in a QTableView

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.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.
  • R Offline
    R Offline
    Rory_1
    wrote on last edited by
    #1

    I'm building a small QTableView (only 20 rows) with two columns: Name and Include. I want to use a QCheckBox in the include column, which is a bool type.

    ui->infoFieldsTable->setModel(mw->infoView->ok);
    for(int row = 0; row < mw->infoView->ok->rowCount(); row++) {
        QModelIndex idx = mw->infoView->ok->index(row,1);
        ui->infoFieldsTable->setIndexWidget(idx, new QCheckBox);
        // set state to match data model value
        ui->infoFieldsTable->indexWidget(idx);  // this is a QWidget, not a QCheckbox, so setCheckState not available
    }
    

    This works to the extent that the table shows the data model information and I can update the data model if I do not add the QCheckBox.

    How do I reference the QCheckBox to update it to the data model value?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      The items supports checkbox with Qt::ItemIsUserCheckable
      for the flags function.

      Qt::ItemFlags yourmodel::flags(const QModelIndex &index) const 
      {
          Qt::ItemFlags result = yourbasemodel::flags(index);
          if (index.column() == COL_U_WANT ) {
              result |= Qt::ItemIsUserCheckable;
          }
          return result;
      }
      

      That said
      it just return as QWidget but you can cast it
      QWidget *wid= ui->infoFieldsTable->indexWidget(idx);
      QCheckBox *box= qobject_cast<QCheckBox *> ( wid )
      if (box) {
      box->xxxx
      }

      R 1 Reply Last reply
      2
      • R Offline
        R Offline
        Rory_1
        wrote on last edited by
        #3

        Thanks. I'll give that a go and report back. Much appreciated!

        1 Reply Last reply
        0
        • mrjjM mrjj

          Hi
          The items supports checkbox with Qt::ItemIsUserCheckable
          for the flags function.

          Qt::ItemFlags yourmodel::flags(const QModelIndex &index) const 
          {
              Qt::ItemFlags result = yourbasemodel::flags(index);
              if (index.column() == COL_U_WANT ) {
                  result |= Qt::ItemIsUserCheckable;
              }
              return result;
          }
          

          That said
          it just return as QWidget but you can cast it
          QWidget *wid= ui->infoFieldsTable->indexWidget(idx);
          QCheckBox *box= qobject_cast<QCheckBox *> ( wid )
          if (box) {
          box->xxxx
          }

          R Offline
          R Offline
          Rory_1
          wrote on last edited by
          #4

          @mrjj Yes, that works. Thank you very much.

          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