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:

    item->pq_tree_item->setCheckable(false);
    item->pq_tree_item->setTristate(false);
    item->pq_tree_item->setUserTristate(false);
    item->pq_tree_item->setEditable(false);
    

    and this:

    item->pq_tree_item->setFlags(0);
    

    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:

    Init:

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

    Hide:

    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 );
    

    Show:

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

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.