1

Mind that this question isn't a duplicate of question Making only one column of a QTreeWidgetItem editable, as it's proposed solution doesn't work.

Hello, so I just want to make only ONE column of my treeWidget editable.

            propertyItems.push_back(new QTreeWidgetItem); //gets filled by the while-loop
            propertyItems[propertyItems.size()-1]->setText(0, prop.name); //sets the text of the item
            propertyItems[propertyItems.size()-1]->setText(1, prop.value);//set the text of the other item
            propertyItems[propertyItems.size()-1]->setFlags(Qt::ItemIsEditable);
            ui->treeWidget_3->insertTopLevelItem(ui->treeWidget_3->topLevelItemCount(), propertyItems[propertyItems.size()-1]); //appends the items
            counter ++;

and

void MainWindow::onTreeWidget3ItemDoubleClicked()
{
    if (ui->treeWidget_3->currentColumn() == 2) {
        ui->treeWidget_3->editItem(ui->treeWidget_3->currentItem(), ui->treeWidget_3->currentColumn());
    }
}

is my approach. ontreeWidget3ItemDoubleClicked is connected with treeWidget::doubleClicked, treeWidget_3 has NO edit-triggers

BUT: when I execute the programm, the QTreeView is just grayed out.

That said, I also tried

propertyItems[propertyItems.size()-1]->setFlags(propertyItems[propertyItem.size()].flags | Qt::ItemIsEditable);

The treeWidget_3 isn't grayed off anymore, but it is still uneditable...

How can I fix this?

BTW: I am a newb to Qt so I might have forgotten something crucial. Sorry in this case.

Community
  • 1
  • 1

1 Answers1

2

As mentioned in the documentation:

The QTreeWidgetItem class provides an item for use with the QTreeWidget convenience class.

It means that it won't work for all use cases. The solution is to create your own model and overload the flags(const QModelIndex& index) method returning the appropriate values (basically Qt:: ItemIsEnabled for read-only columns and Qt:: ItemIsEnabled | Qt::ItemIsEditable for the editable one). You can get the column from index.column().

Qt provides an example to start with trees and models.

cbuchart
  • 10,847
  • 9
  • 53
  • 93