Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making only one column of a QTreeWidgetItem editable

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:

QTreeWidgetItem* item = new QTreeWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);

all columns become editable.

like image 605
Andreas Brinck Avatar asked May 10 '10 10:05

Andreas Brinck


3 Answers

You can make only certain columns in a QTreeWidget editable using a workaround:

1) Set the editTriggers property of the QTreeWidget to NoEditTriggers

2) On inserting items, set the Qt:ItemIsEditable flag of the QTreeWidgetItem object

3) Connect the following slot to the "itemDoubleClicked" signal of the QTreeWidget object:

void MainWindow::onTreeWidgetItemDoubleClicked(QTreeWidgetItem * item, int column)
{
    if (isEditable(column)) {
        ui.treeWidget->editItem(item, column);
    }
}

where "isEditable" is a function you wrote that returns true for editable columns and false for non-editable columns.

like image 168
d11 Avatar answered Nov 18 '22 10:11

d11


I had the same problem recently and discovered a solution which works with all EditTriggers, not only the DoubleClicked one (and the connection to the double clicked signal)

Create a Delegate, that returns a NULL Pointer for the editor:

class NoEditDelegate: public QStyledItemDelegate {
    public:
      NoEditDelegate(QObject* parent=0): QStyledItemDelegate(parent) {}
      virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
        return 0;
      }
    };

And later use it as a custom delegate for your column

ui->parameterView->setItemDelegateForColumn(0, new NoEditDelegate(this));
like image 22
user571167 Avatar answered Nov 18 '22 11:11

user571167


Looks like you will have to forgo using QTreeWidget and QTreeWidgetItem and go with QTreeView and QAbstractItemModel. The "Widget" classes are convenience classes that are concrete implementations of the more abstract but more flexible versions. QAbstractItemModel has a call flags(QModelIndex index) where you would return the appropriate value for your column.

like image 9
Harald Scheirich Avatar answered Nov 18 '22 11:11

Harald Scheirich