Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved How to reference a indexWidget in a QTableView

    General and Desktop
    2
    4
    1094
    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.
    • R
      Rory_1 last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 2
        • R
          Rory_1 last edited by

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

          1 Reply Last reply Reply Quote 0
          • R
            Rory_1 @mrjj last edited by

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

            1 Reply Last reply Reply Quote 1
            • First post
              Last post