Making only one column of a QTreeWidgetItem editable

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

    Seem like the standard QTreeWidget doesn't allow this. I think there are two ways to do this:

    1. Use a QTreeView with your own class derived from QAbstractItemModel and override the flags function

    2. Use a QTreeView with a QStandardItemModel. Then when you add the item just set the appropriate column to allow edits:

    Here's some code for the second option:

    QString x, y;
    QList newIt;
    QStandardItem * item = new QStandardItem(x);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
    newIt.append(item);
    item = new QStandardItem(y);
    item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
    newIt.append(item);
    model->appendRow(newIt);
    

    I find the second approach simpler but that depends on how much flexibility you want with your model.

提交回复
热议问题