Important: Please read the Qt Code of Conduct -

how to hide checkbox of QStandardItem

  • Hello,

    is it possible to hide the checkbox of a QStandardItem if it was checkable in the past?

    I create my items with item->setCheckable(false); and there is no checkbox visible.
    Once i set item->setCheckable(true); the checkbox becomes visible... ok so far.
    The problem is when i try to set an Item to be uncheckable again the checkbox keeps being visible.

    I tried this:


    and this:


    but it doesn't work.

  • item->pq_tree_item->setData(QVariant(),Qt::CheckStateRole);

    Looks kinda pulled out of the hat and probably should be documented better but it's quite clear if you look at the source

  • Thank you! It works like a charm.

    this is the code i am using to backup the previous check state:


    tmp->setData( QVariant(Qt::Checked), ROLE_TREE_BACKUP_CHECKSTATE);
    tmp->setData( QVariant(),Qt::CheckStateRole);
    tmp->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );


    QVariant backup = item->data(Qt::CheckStateRole);
    if( backup.isValid() && !backup.isNull() ) item->setData( backup , ROLE_TREE_BACKUP_CHECKSTATE);
    item->setData( QVariant(),Qt::CheckStateRole);
    item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );


    item->setData(  item->data(ROLE_TREE_BACKUP_CHECKSTATE), Qt::CheckStateRole);
    item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable );

Log in to reply