Making only one column of a QTreeWidgetItem editable

后端 未结 10 1140
无人共我
无人共我 2020-12-06 04:18

I have a QTreeWidgetItem with two columns of data, is there any way to make only the second column editable? When I do the following:

QTreeWidge         


        
10条回答
  •  鱼传尺愫
    2020-12-06 04:45

    Maybe a little late, but may help :

    void MyClass::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column) {
        Qt::ItemFlags flags = item->flags();
        if(column == 0)
        {
            item->setFlags(flags & (~Qt::ItemIsEditable));
        }
        else
        {
            item->setFlags(flags | Qt::ItemIsEditable);
        } 
    }
    

    Here 0 is the index of the column you want to make readonly.

    flags & (~Qt::ItemIsEditable)
    

    Sets the ItemIsEditable position to 0 regardless the previous flag of your item.

    flags | Qt::ItemIsEditable
    

    Sets it to 1 regardless the previous flag.

提交回复
热议问题