Making only one column of a QTreeWidgetItem editable

后端 未结 10 1146
无人共我
无人共我 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 05:05

    The simplest way that I found was to use Qt::ItemFlags

    void myClass::treeDoubleClickSlot(QTreeWidgetItem *item, int column)
    {
        Qt::ItemFlags tmp = item->flags();
        if (isEditable(item, column)) {
            item->setFlags(tmp | Qt::ItemIsEditable);
        } else if (tmp & Qt::ItemIsEditable) {
            item->setFlags(tmp ^ Qt::ItemIsEditable);
        }
    }
    

    The top of the if adds the editing functionality through an OR, and the bottom checks if it is there with AND, then removes it with a XOR.

    This way the editing functionality is added when you want it, and removed when you don't.

    Then connect this function to the tree widget's itemDoubleClicked() signal, and write your 'to edit or not to edit' decision inside of isEditable()

提交回复
热议问题