Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Do not understand the meaning of this statement...



  • Hi!!

    Im wondering why it says "ItemIsEnabled" if this flag disables it...

    ui->myWidgetTableItem->setFlags(Qt::ItemFlag::ItemIsEnabled);
    

  • Lifetime Qt Champion

    Hi,

    This flag does not disable it. From the documentation of ItemIsEnabled: The user can interact with the item.


  • Lifetime Qt Champion

    Hi
    Do note you use setFlags so if you only give it one flag it might have side effects
    Since you then clear out
    Qt::ItemIsSelectable 1 It can be selected.
    Qt::ItemIsEditable 2 It can be edited.

    "The ItemFlags type is a typedef for QFlags<ItemFlag>. It stores an OR combination of ItemFlag values."

    So make sure the other flags are still set if you want it to be selectable etc.

    You can do it as one liner but here is a more verbose example to show how it can
    be used.

    // set the item editable
    item = tableWidget->item(0, 0);
    flags = item->flags();
    flags |= Qt::ItemIsSelectable | Qt::ItemIsEditable; // set the flag
    item->setFlags(flags);
     
     
    // set the item non-editable (view only), but still selectable
    item = tableWidget->item(0, 0);
    flags = item->flags();
    flags |= Qt::ItemIsSelectable;
    flags &= ~Qt::ItemIsEditable; // reset/clear the flag
    item->setFlags(flags);
     
     
    // set the item non-editable (view only), and non-selectable
    item = tableWidget->item(0, 0);
    flags = item->flags();
    flags &= ~(Qt::ItemIsSelectable | Qt::ItemIsEditable); // reset/clear the flag
    item->setFlags(flags);
    


  • All clear.. thanks!


  • Lifetime Qt Champion

    Good !

    In that case, please mark the thread as solved using the "Topic Tools" button so that other forum users may know an answer has been found :-)



  • What @mrjj describes works, but it is also a little bit more complicated than it needs to be. With regular flags (usually defined by enums) this would be the only way. However, QFlags also provides a convenient setter function:

    item = tableWidget->item(0, 0);
    item->setFlags(item->flags().setFlag(Qt::ItemIsSelectable, true).setFlag(Qt::ItemIsEditable, false));
    

    This example would turn on Qt::ItemIsSelectable and turn off Qt::ItemIsEditable. Note that it is possible to chain these together as setFlags(...) immediately returns QFlags again.


Log in to reply